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() != latex_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);
571 #ifdef INHERIT_LANGUAGE
573 if ((font.language() == inherit_language) ||
574 (font.language() == ignore_language))
575 lyxerr << "No this shouldn't happen!\n";
578 if (lyxrc.mark_foreign_language &&
579 font.language() != latex_language &&
580 font.language() != bview->buffer()->params.language) {
581 int const y = offset + row->height() - 1;
582 pain.line(int(tmpx), y, int(x), y,
586 // If we want ulem.sty support, drawing
587 // routines should go here. (Asger)
588 // Why shouldn't LyXFont::drawText handle it internally?
592 // Returns the left beginning of the text.
593 // This information cannot be taken from the layouts-objekt, because in
594 // LaTeX the beginning of the text fits in some cases (for example sections)
595 // exactly the label-width.
596 int LyXText::leftMargin(BufferView * bview, Row const * row) const
598 LyXLayout const & layout =
599 textclasslist.Style(bview->buffer()->params.textclass,
600 row->par()->getLayout());
602 string parindent = layout.parindent;
604 int x = LYX_PAPER_MARGIN;
606 x += lyxfont::signedWidth(textclasslist
607 .TextClass(bview->buffer()->params.textclass)
610 .TextClass(bview->buffer()->params.textclass)
613 // this is the way, LyX handles the LaTeX-Environments.
614 // I have had this idea very late, so it seems to be a
615 // later added hack and this is true
616 if (!row->par()->getDepth()) {
617 if (!row->par()->getLayout()) {
618 // find the previous same level paragraph
619 if (row->par()->previous()) {
620 Paragraph * newpar = row->par()
621 ->depthHook(row->par()->getDepth());
623 textclasslist.Style(bview->buffer()->params.textclass,
630 // find the next level paragraph
633 row->par()->outerHook();
635 // make a corresponding row. Needed to call LeftMargin()
637 // check wether it is a sufficent paragraph
640 .Style(bview->buffer()->params.textclass,
641 newpar->getLayout()).isEnvironment()) {
643 dummyrow.par(newpar);
644 dummyrow.pos(newpar->size());
645 x = leftMargin(bview, &dummyrow);
647 // this is no longer an error, because this function
648 // is used to clear impossible depths after changing
649 // a layout. Since there is always a redo,
650 // LeftMargin() is always called
651 row->par()->params().depth(0);
654 if (newpar && !row->par()->getLayout()) {
655 if (newpar->params().noindent())
658 parindent = textclasslist
659 .Style(bview->buffer()->params.textclass,
660 newpar->getLayout()).parindent;
665 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
666 switch (layout.margintype) {
668 if (!layout.leftmargin.empty()) {
669 x += lyxfont::signedWidth(layout.leftmargin,
671 .TextClass(bview->buffer()->params.
675 if (!row->par()->getLabelstring().empty()) {
676 x += lyxfont::signedWidth(layout.labelindent,
678 x += lyxfont::width(row->par()->getLabelstring(),
680 x += lyxfont::width(layout.labelsep, labelfont);
684 x += lyxfont::signedWidth(layout.labelindent, labelfont);
685 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
686 if (!row->par()->getLabelWidthString().empty()) {
687 x += lyxfont::width(row->par()->getLabelWidthString(),
689 x += lyxfont::width(layout.labelsep, labelfont);
694 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
695 / (row->par()->getDepth() + 4);
697 case MARGIN_FIRST_DYNAMIC:
698 if (layout.labeltype == LABEL_MANUAL) {
699 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
700 x += lyxfont::signedWidth(layout.leftmargin,
703 x += lyxfont::signedWidth(layout.labelindent,
706 } else if (row->pos()
707 // Special case to fix problems with
709 || (layout.labeltype == LABEL_STATIC
710 && layout.latextype == LATEX_ENVIRONMENT
711 && ! row->par()->isFirstInSequence())) {
712 x += lyxfont::signedWidth(layout.leftmargin,
714 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
715 && layout.labeltype != LABEL_BIBLIO
716 && layout.labeltype !=
717 LABEL_CENTERED_TOP_ENVIRONMENT) {
718 x += lyxfont::signedWidth(layout.labelindent,
720 x += lyxfont::width(layout.labelsep, labelfont);
721 x += lyxfont::width(row->par()->getLabelstring(),
726 case MARGIN_RIGHT_ADDRESS_BOX:
728 // ok, a terrible hack. The left margin depends on the widest
729 // row in this paragraph. Do not care about footnotes, they
730 // are *NOT* allowed in the LaTeX realisation of this layout.
732 // find the first row of this paragraph
733 Row const * tmprow = row;
734 while (tmprow->previous()
735 && tmprow->previous()->par() == row->par())
736 tmprow = tmprow->previous();
738 int minfill = tmprow->fill();
739 while (tmprow->next() && tmprow->next()->par() == row->par()) {
740 tmprow = tmprow->next();
741 if (tmprow->fill() < minfill)
742 minfill = tmprow->fill();
745 x += lyxfont::signedWidth(layout.leftmargin,
747 .TextClass(bview->buffer()->params.textclass)
754 int align; // wrong type
756 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
757 align = layout.align;
759 align = row->par()->params().align();
761 // set the correct parindent
762 if (row->pos() == 0) {
763 if ((layout.labeltype == LABEL_NO_LABEL
764 || layout.labeltype == LABEL_TOP_ENVIRONMENT
765 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
766 || (layout.labeltype == LABEL_STATIC
767 && layout.latextype == LATEX_ENVIRONMENT
768 && ! row->par()->isFirstInSequence()))
769 && align == LYX_ALIGN_BLOCK
770 && !row->par()->params().noindent()
771 && (row->par()->layout ||
772 bview->buffer()->params.paragraph_separation ==
773 BufferParams::PARSEP_INDENT))
774 x += lyxfont::signedWidth(parindent,
776 .TextClass(bview->buffer()->params
779 else if (layout.labeltype == LABEL_BIBLIO) {
780 // ale970405 Right width for bibitems
781 x += bibitemMaxWidth(bview,textclasslist
782 .TextClass(bview->buffer()->params
791 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
793 LyXLayout const & layout =
794 textclasslist.Style(buf->params.textclass,
795 row->par()->getLayout());
797 int x = LYX_PAPER_MARGIN
798 + lyxfont::signedWidth(textclasslist
799 .TextClass(buf->params.textclass)
802 .TextClass(buf->params.textclass)
805 // this is the way, LyX handles the LaTeX-Environments.
806 // I have had this idea very late, so it seems to be a
807 // later added hack and this is true
808 if (row->par()->getDepth()) {
809 // find the next level paragraph
811 Paragraph * newpar = row->par();
814 newpar = newpar->previous();
816 && newpar->getDepth() >= row->par()->getDepth());
818 // make a corresponding row. Needed to call LeftMargin()
820 // check wether it is a sufficent paragraph
822 && textclasslist.Style(buf->params.textclass,
826 dummyrow.par(newpar);
828 x = rightMargin(buf, &dummyrow);
830 // this is no longer an error, because this function
831 // is used to clear impossible depths after changing
832 // a layout. Since there is always a redo,
833 // LeftMargin() is always called
834 row->par()->params().depth(0);
838 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
839 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
840 .TextClass(buf->params.textclass)
841 .defaultfont()) * 4 / (row->par()->getDepth()
847 int LyXText::labelEnd(BufferView * bview, Row const * row) const
849 if (textclasslist.Style(bview->buffer()->params.textclass,
850 row->par()->getLayout()).margintype
854 tmprow.pos(row->par()->size());
855 return leftMargin(bview, &tmprow); /* just the beginning
858 return 0; /* LabelEnd is only needed, if the
859 layout fills a flushleft
864 // get the next breakpoint in a given paragraph
866 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
868 Paragraph * par = row->par();
873 Paragraph::size_type const pos = row->pos();
876 // position of the last possible breakpoint
877 // -1 isn't a suitable value, but a flag
878 Paragraph::size_type last_separator = -1;
879 width -= rightMargin(bview->buffer(), row);
881 Paragraph::size_type const main_body =
882 beginningOfMainBody(bview->buffer(), par);
883 LyXLayout const & layout =
884 textclasslist.Style(bview->buffer()->params.textclass, par->getLayout());
885 Paragraph::size_type i = pos;
887 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
888 /* special code for right address boxes, only newlines count */
889 while (i < par->size()) {
890 if (par->isNewline(i)) {
892 i = par->size() - 1; // this means break
894 } else if (par->getChar(i) == Paragraph::META_INSET &&
895 par->getInset(i) && par->getInset(i)->display()){
896 par->getInset(i)->display(false);
901 // Last position is an invariant
902 Paragraph::size_type const last =
904 // this is the usual handling
905 int x = leftMargin(bview, row);
906 bool doitonetime = true;
907 while (doitonetime || ((x < width) && (i < last))) {
909 char const c = par->getChar(i);
910 if (IsNewlineChar(c)) {
912 x = width; // this means break
913 } else if (c == Paragraph::META_INSET &&
916 // check wether a Display() inset is
917 // valid here. if not, change it to
919 if (par->getInset(i)->display() &&
920 (layout.isCommand() ||
921 (layout.labeltype == LABEL_MANUAL
922 && i < beginningOfMainBody(bview->buffer(), par)))) {
923 // display istn't allowd
924 par->getInset(i)->display(false);
925 x += singleWidth(bview, par, i, c);
926 } else if (par->getInset(i)->display() ||
927 par->getInset(i)->needFullRow()) {
928 // So break the line here
932 if (IsLineSeparatorChar(par->getChar(i+1)))
935 last_separator = last; // to avoid extra rows
937 last_separator = i - 1;
938 x = width; // this means break
940 x += singleWidth(bview, par, i, c);
943 if (IsLineSeparatorChar(c))
945 x += singleWidth(bview, par, i, c);
948 if (i == main_body) {
949 x += lyxfont::width(layout.labelsep,
950 getLabelFont(bview->buffer(), par));
951 if (par->isLineSeparator(i - 1))
952 x-= singleWidth(bview, par, i - 1);
953 int left_margin = labelEnd(bview, row);
958 // end of paragraph is always a suitable separator
959 if (i == last && x < width)
963 // well, if last_separator is still 0, the line isn't breakable.
964 // don't care and cut simply at the end
965 if (last_separator < 0) {
969 // manual labels cannot be broken in LaTeX, do not care
970 if (main_body && last_separator < main_body)
971 last_separator = main_body - 1;
973 return last_separator;
977 // returns the minimum space a row needs on the screen in pixel
978 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
984 // get the pure distance
985 Paragraph::size_type const last = rowLastPrintable(row);
987 // special handling of the right address boxes
988 if (textclasslist.Style(bview->buffer()->params.textclass,
989 row->par()->getLayout()).margintype
990 == MARGIN_RIGHT_ADDRESS_BOX) {
991 int const tmpfill = row->fill();
992 row->fill(0); // the minfill in MarginLeft()
993 w = leftMargin(bview, row);
996 w = leftMargin(bview, row);
998 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
999 row->par()->getLayout());
1000 Paragraph::size_type const main_body =
1001 beginningOfMainBody(bview->buffer(), row->par());
1002 Paragraph::size_type i = row->pos();
1005 if (main_body > 0 && i == main_body) {
1006 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1007 if (row->par()->isLineSeparator(i - 1))
1008 w -= singleWidth(bview, row->par(), i - 1);
1009 int left_margin = labelEnd(bview, row);
1010 if (w < left_margin)
1013 w += singleWidth(bview, row->par(), i);
1016 if (main_body > 0 && main_body > last) {
1017 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1018 if (last >= 0 && row->par()->isLineSeparator(last))
1019 w -= singleWidth(bview, row->par(), last);
1020 int const left_margin = labelEnd(bview, row);
1021 if (w < left_margin)
1025 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1026 #ifdef WITH_WARNINGS
1027 #warning Please fix me (Jug!)
1037 // returns the minimum space a manual label needs on the screen in pixel
1038 int LyXText::labelFill(BufferView * bview, Row const * row) const
1040 Paragraph::size_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1041 // -1 because a label ends either with a space that is in the label,
1042 // or with the beginning of a footnote that is outside the label.
1044 // I don't understand this code in depth, but sometimes "last" is
1045 // less than 0 and this causes a crash. This fix seems to work
1046 // correctly, but I bet the real error is elsewhere. The bug is
1047 // triggered when you have an open footnote in a paragraph
1048 // environment with a manual label. (Asger)
1049 if (last < 0) last = 0;
1051 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1058 w += singleWidth(bview, row->par(), i);
1063 if (!row->par()->params().labelWidthString().empty()) {
1064 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1065 getLabelFont(bview->buffer(), row->par())) - w,
1073 // returns the number of separators in the specified row. The separator
1074 // on the very last column doesnt count
1075 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1077 Paragraph::size_type const last = rowLast(row);
1078 Paragraph::size_type p =
1079 max(row->pos(), beginningOfMainBody(buf, row->par()));
1081 for (; p < last; ++p) {
1082 if (row->par()->isSeparator(p)) {
1090 // returns the number of hfills in the specified row. The LyX-Hfill is
1091 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1092 // ignored. This is *MUCH* more usefull than not to ignore!
1093 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1095 Paragraph::size_type const last = rowLast(row);
1096 Paragraph::size_type first = row->pos();
1097 if (first) { /* hfill *DO* count at the beginning
1099 while(first <= last && row->par()->isHfill(first))
1103 first = max(first, beginningOfMainBody(buf, row->par()));
1105 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1106 if (row->par()->isHfill(p)) {
1114 // like NumberOfHfills, but only those in the manual label!
1115 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1117 Paragraph::size_type last = rowLast(row);
1118 Paragraph::size_type first = row->pos();
1119 if (first) { /* hfill *DO* count at the beginning
1121 while(first < last && row->par()->isHfill(first))
1125 last = min(last, beginningOfMainBody(buf, row->par()));
1127 for (Paragraph::size_type p = first;
1128 p < last; ++p) { // last, because the end is ignored!
1129 if (row->par()->isHfill(p)) {
1137 // returns true, if a expansion is needed.
1138 // Rules are given by LaTeX
1139 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1140 Paragraph::size_type pos) const
1142 // by the way, is it a hfill?
1143 if (!row_ptr->par()->isHfill(pos))
1146 // at the end of a row it does not count
1147 if (pos >= rowLast(row_ptr))
1150 // at the beginning of a row it does not count, if it is not
1151 // the first row of a paragaph
1152 if (!row_ptr->pos())
1155 // in some labels it does not count
1156 if (textclasslist.Style(buf->params.textclass,
1157 row_ptr->par()->getLayout()).margintype
1159 && pos < beginningOfMainBody(buf, row_ptr->par()))
1162 // if there is anything between the first char of the row and
1163 // the sepcified position that is not a newline and not a hfill,
1164 // the hfill will count, otherwise not
1165 Paragraph::size_type i = row_ptr->pos();
1166 while (i < pos && (row_ptr->par()->isNewline(i)
1167 || row_ptr->par()->isHfill(i)))
1174 LColor::color LyXText::backgroundColor()
1177 return inset_owner->backgroundColor();
1179 return LColor::background;
1182 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1184 /* get the maximum ascent and the maximum descent */
1187 float layoutasc = 0;
1188 float layoutdesc = 0;
1191 Inset * tmpinset = 0;
1193 /* this must not happen before the currentrow for clear reasons.
1194 so the trick is just to set the current row onto this row */
1196 getRow(row_ptr->par(), row_ptr->pos(), unused_y);
1198 /* ok , let us initialize the maxasc and maxdesc value.
1199 * This depends in LaTeX of the font of the last character
1200 * in the paragraph. The hack below is necessary because
1201 * of the possibility of open footnotes */
1203 /* Correction: only the fontsize count. The other properties
1204 are taken from the layoutfont. Nicer on the screen :) */
1205 Paragraph * par = row_ptr->par();
1206 Paragraph * firstpar = row_ptr->par();
1208 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1209 firstpar->getLayout());
1211 // as max get the first character of this row then it can increes but not
1212 // decrees the height. Just some point to start with so we don't have to
1213 // do the assignment below too often.
1214 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1215 LyXFont::FONT_SIZE const tmpsize = font.size();
1216 font = getLayoutFont(bview->buffer(), par);
1217 LyXFont::FONT_SIZE const size = font.size();
1218 font.setSize(tmpsize);
1220 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1222 float spacing_val = 1.0;
1223 if (!row_ptr->par()->params().spacing().isDefault()) {
1224 spacing_val = row_ptr->par()->params().spacing().getValue();
1226 spacing_val = bview->buffer()->params.spacing.getValue();
1228 //lyxerr << "spacing_val = " << spacing_val << endl;
1230 int maxasc = int(lyxfont::maxAscent(font) *
1231 layout.spacing.getValue() *
1233 int maxdesc = int(lyxfont::maxDescent(font) *
1234 layout.spacing.getValue() *
1236 int const pos_end = rowLast(row_ptr);
1240 // Check if any insets are larger
1241 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1242 if (row_ptr->par()->getChar(pos) == Paragraph::META_INSET) {
1243 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1244 tmpinset = row_ptr->par()->getInset(pos);
1246 #if 1 // this is needed for deep update on initialitation
1247 tmpinset->update(bview, tmpfont);
1249 asc = tmpinset->ascent(bview, tmpfont);
1250 desc = tmpinset->descent(bview, tmpfont);
1251 maxwidth += tmpinset->width(bview, tmpfont);
1252 maxasc = max(maxasc, asc);
1253 maxdesc = max(maxdesc, desc);
1256 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1260 // Check if any custom fonts are larger (Asger)
1261 // This is not completely correct, but we can live with the small,
1262 // cosmetic error for now.
1263 LyXFont::FONT_SIZE maxsize =
1264 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1265 if (maxsize > font.size()) {
1266 font.setSize(maxsize);
1268 asc = lyxfont::maxAscent(font);
1269 desc = lyxfont::maxDescent(font);
1276 // This is nicer with box insets:
1280 row_ptr->ascent_of_text(maxasc);
1282 // is it a top line?
1283 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1285 // some parksips VERY EASY IMPLEMENTATION
1286 if (bview->buffer()->params.paragraph_separation ==
1287 BufferParams::PARSEP_SKIP)
1289 if (layout.isParagraph()
1290 && firstpar->getDepth() == 0
1291 && firstpar->previous())
1293 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1294 } else if (firstpar->previous() &&
1295 textclasslist.Style(bview->buffer()->params.textclass,
1296 firstpar->previous()->
1297 getLayout()).isParagraph() &&
1298 firstpar->previous()->getDepth() == 0)
1300 // is it right to use defskip here too? (AS)
1301 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1305 // the paper margins
1306 if (!row_ptr->par()->previous() && bv_owner)
1307 maxasc += LYX_PAPER_MARGIN;
1309 // add the vertical spaces, that the user added
1310 if (firstpar->params().spaceTop().kind() != VSpace::NONE)
1311 maxasc += int(firstpar->params().spaceTop().inPixels(bview));
1313 // do not forget the DTP-lines!
1314 // there height depends on the font of the nearest character
1315 if (firstpar->params().lineTop())
1316 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1319 // and now the pagebreaks
1320 if (firstpar->params().pagebreakTop())
1321 maxasc += 3 * defaultHeight();
1323 // This is special code for the chapter, since the label of this
1324 // layout is printed in an extra row
1325 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1326 && bview->buffer()->params.secnumdepth >= 0)
1328 float spacing_val = 1.0;
1329 if (!row_ptr->par()->params().spacing().isDefault()) {
1330 spacing_val = row_ptr->par()->params().spacing().getValue();
1332 spacing_val = bview->buffer()->params.spacing.getValue();
1335 labeladdon = int(lyxfont::maxDescent(labelfont) *
1336 layout.spacing.getValue() *
1338 + int(lyxfont::maxAscent(labelfont) *
1339 layout.spacing.getValue() *
1343 // special code for the top label
1344 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1345 || layout.labeltype == LABEL_BIBLIO
1346 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1347 && row_ptr->par()->isFirstInSequence()
1348 && !row_ptr->par()->getLabelstring().empty())
1350 float spacing_val = 1.0;
1351 if (!row_ptr->par()->params().spacing().isDefault()) {
1352 spacing_val = row_ptr->par()->params().spacing().getValue();
1354 spacing_val = bview->buffer()->params.spacing.getValue();
1358 (lyxfont::maxAscent(labelfont) *
1359 layout.spacing.getValue() *
1361 +(lyxfont::maxDescent(labelfont) *
1362 layout.spacing.getValue() *
1364 + layout.topsep * defaultHeight()
1365 + layout.labelbottomsep * defaultHeight());
1368 // and now the layout spaces, for example before and after a section,
1369 // or between the items of a itemize or enumerate environment
1371 if (!firstpar->params().pagebreakTop()) {
1372 Paragraph * prev = row_ptr->par()->previous();
1374 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1375 if (prev && prev->getLayout() == firstpar->getLayout() &&
1376 prev->getDepth() == firstpar->getDepth() &&
1377 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1379 layoutasc = (layout.itemsep * defaultHeight());
1380 } else if (row_ptr->previous()) {
1381 tmptop = layout.topsep;
1383 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1384 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1385 row_ptr->previous()->par()->
1386 getLayout()).bottomsep;
1389 layoutasc = (tmptop * defaultHeight());
1390 } else if (row_ptr->par()->params().lineTop()) {
1391 tmptop = layout.topsep;
1394 layoutasc = (tmptop * defaultHeight());
1397 prev = row_ptr->par()->outerHook();
1399 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1400 prev->getLayout()).parsep * defaultHeight());
1402 if (firstpar->previous() &&
1403 firstpar->previous()->getDepth() == 0 &&
1404 firstpar->previous()->getLayout() !=
1405 firstpar->getLayout())
1408 } else if (firstpar->previous()) {
1409 maxasc += int(layout.parsep * defaultHeight());
1415 // is it a bottom line?
1416 if (row_ptr->par() == par
1417 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1419 // the paper margins
1420 if (!par->next() && bv_owner)
1421 maxdesc += LYX_PAPER_MARGIN;
1423 // add the vertical spaces, that the user added
1424 if (firstpar->params().spaceBottom().kind() != VSpace::NONE)
1425 maxdesc += int(firstpar->params().spaceBottom().inPixels(bview));
1427 // do not forget the DTP-lines!
1428 // there height depends on the font of the nearest character
1429 if (firstpar->params().lineBottom())
1430 maxdesc += 2 * lyxfont::ascent('x',
1431 getFont(bview->buffer(),
1432 par, par->size() - 1));
1434 // and now the pagebreaks
1435 if (firstpar->params().pagebreakBottom())
1436 maxdesc += 3 * defaultHeight();
1438 // and now the layout spaces, for example before and after
1439 // a section, or between the items of a itemize or enumerate
1441 if (!firstpar->params().pagebreakBottom() && row_ptr->par()->next()) {
1442 Paragraph * nextpar = row_ptr->par()->next();
1443 Paragraph * comparepar = row_ptr->par();
1447 if (comparepar->getDepth() > nextpar->getDepth()) {
1448 usual = (textclasslist.Style(bview->buffer()->params.textclass,
1449 comparepar->getLayout()).bottomsep * defaultHeight());
1450 comparepar = comparepar->depthHook(nextpar->getDepth());
1451 if (comparepar->getLayout()!= nextpar->getLayout()
1452 || nextpar->getLabelWidthString() !=
1453 comparepar->getLabelWidthString())
1455 unusual = (textclasslist.Style(bview->buffer()->params.textclass,
1456 comparepar->getLayout()).bottomsep * defaultHeight());
1458 if (unusual > usual)
1459 layoutdesc = unusual;
1462 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1464 if (comparepar->getLayout()!= nextpar->getLayout()
1465 || nextpar->getLabelWidthString() !=
1466 comparepar->getLabelWidthString())
1467 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
1468 comparepar->getLayout()).bottomsep * defaultHeight());
1473 // incalculate the layout spaces
1474 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1475 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1477 // calculate the new height of the text
1478 height -= row_ptr->height();
1480 row_ptr->height(maxasc + maxdesc + labeladdon);
1481 row_ptr->baseline(maxasc + labeladdon);
1483 height += row_ptr->height();
1486 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1487 row_ptr->width(int(maxwidth + x));
1490 width = max(0,workWidth(bview));
1492 if (r->width() > width)
1500 /* Appends the implicit specified paragraph behind the specified row,
1501 * start at the implicit given position */
1502 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1504 bool not_ready = true;
1506 // The last character position of a paragraph is an invariant so we can
1507 // safely get it here. (Asger)
1508 int const lastposition = row->par()->size();
1510 // Get the next breakpoint
1511 int z = nextBreakPoint(bview, row, workWidth(bview));
1515 // Insert the new row
1516 if (z < lastposition) {
1518 insertRow(row, row->par(), z);
1525 // Set the dimensions of the row
1526 #ifdef WITH_WARNINGS
1527 #warning Something is rotten here! (Jug)
1529 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1530 setHeightOfRow(bview, tmprow);
1532 } while (not_ready);
1536 void LyXText::breakAgain(BufferView * bview, Row * row) const
1538 bool not_ready = true;
1541 // get the next breakpoint
1542 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1545 if (z < row->par()->size()) {
1546 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1549 insertRow(row, row->par(), z);
1555 if (row->pos() == z)
1556 not_ready = false; // the rest will not change
1562 /* if there are some rows too much, delete them */
1563 /* only if you broke the whole paragraph! */
1564 Row * tmprow2 = row;
1565 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1566 tmprow2 = tmprow2->next();
1568 while (tmprow2 != row) {
1569 tmprow2 = tmprow2->previous();
1570 removeRow(tmprow2->next());
1575 /* set the dimensions of the row */
1576 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1577 setHeightOfRow(bview, tmprow);
1578 } while (not_ready);
1582 // this is just a little changed version of break again
1583 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1585 // get the next breakpoint
1586 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1589 if (z < row->par()->size()) {
1591 || (row->next() && row->next()->par() != row->par())) {
1592 /* insert a new row */
1594 insertRow(row, row->par(), z);
1600 if (row->pos() != z)
1604 // if there are some rows too much, delete them
1605 // only if you broke the whole paragraph!
1606 Row * tmprow2 = row;
1607 while (tmprow2->next()
1608 && tmprow2->next()->par() == row->par()) {
1609 tmprow2 = tmprow2->next();
1611 while (tmprow2 != row) {
1612 tmprow2 = tmprow2->previous();
1613 removeRow(tmprow2->next());
1617 // set the dimensions of the row
1618 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1619 setHeightOfRow(bview, tmprow);
1623 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1625 LyXLayout const & layout =
1626 textclasslist.Style(bview->buffer()->params.textclass,
1627 cursor.par()->getLayout());
1629 // this is only allowed, if the current paragraph is not empty or caption
1630 if ((cursor.par()->size() <= 0)
1631 && layout.labeltype!= LABEL_SENSITIVE)
1634 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1636 // Always break behind a space
1638 // It is better to erase the space (Dekel)
1639 if (cursor.pos() < cursor.par()->size()
1640 && cursor.par()->isLineSeparator(cursor.pos()))
1641 cursor.par()->erase(cursor.pos());
1642 // cursor.pos(cursor.pos() + 1);
1644 // break the paragraph
1648 keep_layout = layout.isEnvironment();
1649 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1652 // well this is the caption hack since one caption is really enough
1653 if (layout.labeltype == LABEL_SENSITIVE) {
1655 // set to standard-layout
1656 cursor.par()->setLayout(0);
1658 // set to standard-layout
1659 cursor.par()->next()->setLayout(0);
1662 /* if the cursor is at the beginning of a row without prior newline,
1664 * This touches only the screen-update. Otherwise we would may have
1665 * an empty row on the screen */
1666 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1667 && cursor.row()->pos() == cursor.pos()) {
1671 status(bview, LyXText::NEED_MORE_REFRESH);
1672 refresh_row = cursor.row();
1673 refresh_y = cursor.y() - cursor.row()->baseline();
1675 // Do not forget the special right address boxes
1676 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1677 while (refresh_row->previous() &&
1678 refresh_row->previous()->par() == refresh_row->par()) {
1679 refresh_row = refresh_row->previous();
1680 refresh_y -= refresh_row->height();
1683 removeParagraph(cursor.row());
1685 // set the dimensions of the cursor row
1686 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1688 setHeightOfRow(bview, cursor.row());
1690 while (cursor.par()->next()->size()
1691 && cursor.par()->next()->isNewline(0))
1692 cursor.par()->next()->erase(0);
1694 insertParagraph(bview, cursor.par()->next(), cursor.row());
1696 updateCounters(bview, cursor.row()->previous());
1698 /* This check is necessary. Otherwise the new empty paragraph will
1699 * be deleted automatically. And it is more friendly for the user! */
1701 setCursor(bview, cursor.par()->next(), 0);
1703 setCursor(bview, cursor.par(), 0);
1705 if (cursor.row()->next())
1706 breakAgain(bview, cursor.row()->next());
1712 // Just a macro to make some thing easier.
1713 void LyXText::redoParagraph(BufferView * bview) const
1716 redoParagraphs(bview, cursor, cursor.par()->next());
1717 setCursorIntern(bview, cursor.par(), cursor.pos());
1721 /* insert a character, moves all the following breaks in the
1722 * same Paragraph one to the right and make a rebreak */
1723 void LyXText::insertChar(BufferView * bview, char c)
1725 setUndo(bview, Undo::INSERT,
1726 cursor.par(), cursor.par()->next());
1728 // When the free-spacing option is set for the current layout,
1729 // disable the double-space checking
1731 bool const freeSpacing =
1732 textclasslist.Style(bview->buffer()->params.textclass,
1733 cursor.row()->par()->getLayout()).free_spacing;
1736 if (lyxrc.auto_number) {
1737 static string const number_operators = "+-/*";
1738 static string const number_unary_operators = "+-";
1739 static string const number_seperators = ".,:";
1741 if (current_font.number() == LyXFont::ON) {
1742 if (!isdigit(c) && !contains(number_operators, c) &&
1743 !(contains(number_seperators, c) &&
1744 cursor.pos() >= 1 &&
1745 cursor.pos() < cursor.par()->size() &&
1746 getFont(bview->buffer(),
1748 cursor.pos()).number() == LyXFont::ON &&
1749 getFont(bview->buffer(),
1751 cursor.pos() - 1).number() == LyXFont::ON)
1753 number(bview); // Set current_font.number to OFF
1754 } else if (isdigit(c) &&
1755 real_current_font.isVisibleRightToLeft()) {
1756 number(bview); // Set current_font.number to ON
1758 if (cursor.pos() > 0) {
1759 char const c = cursor.par()->getChar(cursor.pos() - 1);
1760 if (contains(number_unary_operators, c) &&
1761 (cursor.pos() == 1 ||
1762 cursor.par()->isSeparator(cursor.pos() - 2) ||
1763 cursor.par()->isNewline(cursor.pos() - 2) )
1765 setCharFont(bview->buffer(),
1769 } else if (contains(number_seperators, c) &&
1770 cursor.pos() >= 2 &&
1771 getFont(bview->buffer(),
1773 cursor.pos() - 2).number() == LyXFont::ON) {
1774 setCharFont(bview->buffer(),
1784 /* First check, if there will be two blanks together or a blank at
1785 the beginning of a paragraph.
1786 I decided to handle blanks like normal characters, the main
1787 difference are the special checks when calculating the row.fill
1788 (blank does not count at the end of a row) and the check here */
1790 // The bug is triggered when we type in a description environment:
1791 // The current_font is not changed when we go from label to main text
1792 // and it should (along with realtmpfont) when we type the space.
1793 // CHECK There is a bug here! (Asger)
1795 LyXFont realtmpfont = real_current_font;
1796 LyXFont rawtmpfont = current_font; /* store the current font.
1797 * This is because of the use
1798 * of cursor movements. The moving
1799 * cursor would refresh the
1802 // Get the font that is used to calculate the baselineskip
1803 Paragraph::size_type const lastpos = cursor.par()->size();
1804 LyXFont rawparfont =
1805 cursor.par()->getFontSettings(bview->buffer()->params,
1808 bool jumped_over_space = false;
1810 if (!freeSpacing && IsLineSeparatorChar(c)) {
1811 if ((cursor.pos() > 0
1812 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1813 || (cursor.pos() > 0
1814 && cursor.par()->isNewline(cursor.pos() - 1))
1815 || (cursor.pos() == 0)) {
1816 static bool sent_space_message = false;
1817 if (!sent_space_message) {
1818 if (cursor.pos() == 0)
1819 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1821 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1822 sent_space_message = true;
1827 } else if (IsNewlineChar(c)) {
1828 if (cursor.par() == cursor.par()
1829 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1833 /* No newline at first position
1834 * of a paragraph or behind labels.
1835 * TeX does not allow that. */
1837 if (cursor.pos() < cursor.par()->size() &&
1838 cursor.par()->isLineSeparator(cursor.pos()))
1839 // newline always after a blank!
1841 cursor.row()->fill(-1); // to force a new break
1844 // the display inset stuff
1845 if (cursor.row()->par()->getChar(cursor.row()->pos()) == Paragraph::META_INSET
1846 && cursor.row()->par()->getInset(cursor.row()->pos())
1847 && (cursor.row()->par()->getInset(cursor.row()->pos())->display() ||
1848 cursor.row()->par()->getInset(cursor.row()->pos())->needFullRow()))
1849 cursor.row()->fill(-1); // to force a new break
1851 // get the cursor row fist
1852 Row * row = cursor.row();
1853 int y = cursor.y() - row->baseline();
1854 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1855 * already insertet the character */
1856 cursor.par()->insertChar(cursor.pos(), c);
1857 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1859 if (!jumped_over_space) {
1860 // refresh the positions
1862 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1863 tmprow = tmprow->next();
1864 tmprow->pos(tmprow->pos() + 1);
1868 // Is there a break one row above
1869 if ((cursor.par()->isLineSeparator(cursor.pos())
1870 || cursor.par()->isNewline(cursor.pos())
1871 || cursor.row()->fill() == -1)
1872 && row->previous() && row->previous()->par() == row->par()) {
1873 Paragraph::size_type z = nextBreakPoint(bview,
1876 if (z >= row->pos()) {
1879 // set the dimensions of the row above
1880 row->previous()->fill(fill(bview,
1884 setHeightOfRow(bview, row->previous());
1886 y -= row->previous()->height();
1888 refresh_row = row->previous();
1889 status(bview, LyXText::NEED_MORE_REFRESH);
1891 breakAgainOneRow(bview, row);
1893 current_font = rawtmpfont;
1894 real_current_font = realtmpfont;
1895 setCursor(bview, cursor.par(), cursor.pos() + 1,
1896 false, cursor.boundary());
1897 // cursor MUST be in row now.
1899 if (row->next() && row->next()->par() == row->par())
1900 need_break_row = row->next();
1904 // check, wether the last characters font has changed.
1905 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1906 && rawparfont != rawtmpfont)
1907 redoHeightOfParagraph(bview, cursor);
1914 // recalculate the fill of the row
1915 if (row->fill() >= 0) /* needed because a newline
1916 * will set fill to -1. Otherwise
1917 * we would not get a rebreak! */
1918 row->fill(fill(bview, row, workWidth(bview)));
1919 if (row->fill() < 0) {
1922 refresh_x = cursor.x();
1923 refresh_pos = cursor.pos();
1924 status(bview, LyXText::NEED_MORE_REFRESH);
1925 breakAgainOneRow(bview, row);
1926 // will the cursor be in another row now?
1927 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1928 if (row->next() && row->next()->par() == row->par())
1929 // this should always be true
1931 breakAgainOneRow(bview, row);
1933 current_font = rawtmpfont;
1934 real_current_font = realtmpfont;
1936 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1938 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1939 != cursor.boundary())
1940 setCursor(bview, cursor.par(), cursor.pos(), false,
1941 !cursor.boundary());
1942 if (row->next() && row->next()->par() == row->par())
1943 need_break_row = row->next();
1948 refresh_x = cursor.x();
1950 refresh_pos = cursor.pos();
1952 int const tmpheight = row->height();
1953 setHeightOfRow(bview, row);
1954 if (tmpheight == row->height())
1955 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
1957 status(bview, LyXText::NEED_MORE_REFRESH);
1959 current_font = rawtmpfont;
1960 real_current_font = realtmpfont;
1961 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1965 // check, wether the last characters font has changed.
1966 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1967 && rawparfont != rawtmpfont) {
1968 redoHeightOfParagraph(bview, cursor);
1970 // now the special right address boxes
1971 if (textclasslist.Style(bview->buffer()->params.textclass,
1972 cursor.par()->getLayout()).margintype
1973 == MARGIN_RIGHT_ADDRESS_BOX) {
1974 redoDrawingOfParagraph(bview, cursor);
1982 void LyXText::charInserted()
1984 // Here we could call FinishUndo for every 20 characters inserted.
1985 // This is from my experience how emacs does it.
1986 static unsigned int counter;
1996 void LyXText::prepareToPrint(BufferView * bview,
1997 Row * row, float & x,
1998 float & fill_separator,
2000 float & fill_label_hfill,
2006 float w = row->fill();
2008 fill_label_hfill = 0;
2010 fill_label_hfill = 0;
2013 row->par()->isRightToLeftPar(bview->buffer()->params);
2015 x = (workWidth(bview) > 0)
2016 ? rightMargin(bview->buffer(), row) : 0;
2018 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2020 // is there a manual margin with a manual label
2021 if (textclasslist.Style(bview->buffer()->params.textclass,
2022 row->par()->getLayout()).margintype == MARGIN_MANUAL
2023 && textclasslist.Style(bview->buffer()->params.textclass,
2024 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
2026 /* one more since labels are left aligned */
2027 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2028 if (nlh && !row->par()->getLabelWidthString().empty()) {
2029 fill_label_hfill = labelFill(bview, row) / nlh;
2033 // are there any hfills in the row?
2034 float const nh = numberOfHfills(bview->buffer(), row);
2038 fill_hfill = w / nh;
2040 // is it block, flushleft or flushright?
2041 // set x how you need it
2043 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2044 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2046 align = row->par()->params().align();
2049 // center displayed insets
2051 if (row->par()->getChar(row->pos()) == Paragraph::META_INSET
2052 && (inset=row->par()->getInset(row->pos()))
2053 && (inset->display())) // || (inset->scroll() < 0)))
2054 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2055 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2058 case LYX_ALIGN_BLOCK:
2059 ns = numberOfSeparators(bview->buffer(), row);
2060 if (ns && row->next() && row->next()->par() == row->par() &&
2061 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2062 && !(row->next()->par()->getChar(row->next()->pos()) == Paragraph::META_INSET
2063 && row->next()->par()->getInset(row->next()->pos())
2064 && row->next()->par()->getInset(row->next()->pos())->display())
2067 fill_separator = w / ns;
2068 } else if (is_rtl) {
2072 case LYX_ALIGN_RIGHT:
2075 case LYX_ALIGN_CENTER:
2083 computeBidiTables(bview->buffer(), row);
2085 Paragraph::size_type main_body =
2086 beginningOfMainBody(bview->buffer(), row->par());
2087 Paragraph::size_type last = rowLast(row);
2089 if (main_body > 0 &&
2090 (main_body-1 > last ||
2091 !row->par()->isLineSeparator(main_body-1))) {
2092 LyXLayout const & layout =
2093 textclasslist.Style(bview->buffer()->params.textclass,
2094 row->par()->getLayout());
2095 x += lyxfont::width(layout.labelsep,
2096 getLabelFont(bview->buffer(), row->par()));
2097 if (main_body-1 <= last)
2098 x += fill_label_hfill;
2103 /* important for the screen */
2106 /* the cursor set functions have a special mechanism. When they
2107 * realize, that you left an empty paragraph, they will delete it.
2108 * They also delete the corresponding row */
2110 void LyXText::cursorRightOneWord(BufferView * bview) const
2112 // treat floats, HFills and Insets as words
2113 LyXCursor tmpcursor = cursor;
2114 // CHECK See comment on top of text.C
2116 if (tmpcursor.pos() == tmpcursor.par()->size()
2117 && tmpcursor.par()->next()) {
2118 tmpcursor.par(tmpcursor.par()->next());
2123 // Skip through initial nonword stuff.
2124 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2125 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2126 // printf("Current pos1 %d", tmpcursor.pos()) ;
2127 tmpcursor.pos(tmpcursor.pos() + 1);
2130 // Advance through word.
2131 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2132 tmpcursor.par()->isWord( tmpcursor.pos())) {
2133 // printf("Current pos2 %d", tmpcursor.pos()) ;
2134 tmpcursor.pos(tmpcursor.pos() + 1);
2138 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2142 void LyXText::cursorTab(BufferView * bview) const
2144 LyXCursor tmpcursor = cursor;
2145 while (tmpcursor.pos() < tmpcursor.par()->size()
2146 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2147 tmpcursor.pos(tmpcursor.pos() + 1);
2149 if (tmpcursor.pos() == tmpcursor.par()->size()){
2150 if (tmpcursor.par()->next()) {
2151 tmpcursor.par(tmpcursor.par()->next());
2155 tmpcursor.pos(tmpcursor.pos() + 1);
2156 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2160 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2161 of prior word, not to end of next prior word. */
2163 void LyXText::cursorLeftOneWord(BufferView * bview) const
2165 LyXCursor tmpcursor = cursor;
2166 cursorLeftOneWord(tmpcursor);
2167 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2170 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2172 // treat HFills, floats and Insets as words
2175 && (cur.par()->isSeparator(cur.pos() - 1)
2176 || cur.par()->isKomma(cur.pos() - 1))
2177 && !(cur.par()->isHfill(cur.pos() - 1)
2178 || cur.par()->isInset(cur.pos() - 1)))
2179 cur.pos(cur.pos() - 1);
2182 && (cur.par()->isInset(cur.pos() - 1)
2183 || cur.par()->isHfill(cur.pos() - 1))) {
2184 cur.pos(cur.pos() - 1);
2185 } else if (!cur.pos()) {
2186 if (cur.par()->previous()){
2187 cur.par(cur.par()->previous());
2188 cur.pos(cur.par()->size());
2190 } else { // Here, cur != 0
2191 while (cur.pos() > 0 &&
2192 cur.par()->isWord(cur.pos()-1) )
2193 cur.pos(cur.pos() - 1);
2197 /* -------> Select current word. This depends on behaviour of
2198 CursorLeftOneWord(), so it is patched as well. */
2199 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2200 word_location const loc) const
2202 // first put the cursor where we wana start to select the word
2205 case WHOLE_WORD_STRICT:
2206 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2207 || cursor.par()->isSeparator(cursor.pos())
2208 || cursor.par()->isKomma(cursor.pos())
2209 || cursor.par()->isSeparator(cursor.pos() -1)
2210 || cursor.par()->isKomma(cursor.pos() -1)) {
2214 // no break here, we go to the next
2217 // Move cursor to the beginning, when not already there.
2218 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2219 && !from.par()->isKomma(from.pos() - 1))
2220 cursorLeftOneWord(from);
2223 // always move the cursor to the beginning of previous word
2224 cursorLeftOneWord(from);
2227 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2233 while (to.pos() < to.par()->size()
2234 && !to.par()->isSeparator(to.pos())
2235 && !to.par()->isKomma(to.pos())
2236 && !to.par()->isHfill(to.pos()) )
2238 to.pos(to.pos() + 1);
2243 void LyXText::selectWord(BufferView * bview, word_location const loc)
2247 getWord(from, to, loc);
2249 setCursor(bview, from.par(), from.pos());
2252 selection.cursor = cursor;
2253 setCursor(bview, to.par(), to.pos() );
2254 setSelection(bview);
2258 /* -------> Select the word currently under the cursor when no
2259 selection is currently set */
2260 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2261 word_location const loc)
2263 if (!selection.set()) {
2264 selectWord(bview, loc);
2265 return selection.set();
2271 // This function is only used by the spellchecker for NextWord().
2272 // It doesn't handle LYX_ACCENTs and probably never will.
2273 string const LyXText::selectNextWord(BufferView * bview,
2274 float & value) const
2276 if (the_locking_inset) {
2277 string str = the_locking_inset->selectNextWord(bview, value);
2279 value += float(cursor.y())/float(height);
2282 #warning Dekel please have a look on this one RTL? (Jug)
2283 // we have to go on checking so move cusor to the right
2284 if (cursor.pos() == cursor.par()->size()) {
2285 if (!cursor.par()->next())
2287 cursor.par(cursor.par()->next());
2290 cursor.pos(cursor.pos() + 1);
2292 Paragraph * tmppar = cursor.par();
2294 // If this is not the very first word, skip rest of
2295 // current word because we are probably in the middle
2296 // of a word if there is text here.
2297 if (cursor.pos() || cursor.par()->previous()) {
2298 while (cursor.pos() < cursor.par()->size()
2299 && cursor.par()->isLetter(cursor.pos()))
2300 cursor.pos(cursor.pos() + 1);
2303 // Now, skip until we have real text (will jump paragraphs)
2304 while ((cursor.par()->size() > cursor.pos()
2305 && (!cursor.par()->isLetter(cursor.pos()))
2306 && (!cursor.par()->isInset(cursor.pos()) ||
2307 !cursor.par()->getInset(cursor.pos())->isTextInset()))
2308 || (cursor.par()->size() == cursor.pos()
2309 && cursor.par()->next()))
2311 if (cursor.pos() == cursor.par()->size()) {
2312 cursor.par(cursor.par()->next());
2315 cursor.pos(cursor.pos() + 1);
2318 // now check if we hit an inset so it has to be a inset containing text!
2319 if (cursor.pos() < cursor.par()->size() &&
2320 cursor.par()->isInset(cursor.pos()))
2323 cursor.par()->getInset(cursor.pos())->edit(bview);
2324 // now call us again to do the above trick
2325 // but obviously we have to start from down below ;)
2326 return bview->text->selectNextWord(bview, value);
2329 // Update the value if we changed paragraphs
2330 if (cursor.par() != tmppar){
2331 setCursor(bview, cursor.par(), cursor.pos());
2332 value = float(cursor.y())/float(height);
2335 // Start the selection from here
2336 selection.cursor = cursor;
2340 // and find the end of the word
2341 // (optional hyphens are part of a word)
2342 while (cursor.pos() < cursor.par()->size()
2343 && (cursor.par()->isLetter(cursor.pos()))
2344 // assignment is intentional here
2345 || ((inset = getInset())
2346 && inset->lyxCode() == Inset::SPECIALCHAR_CODE
2347 && static_cast<InsetSpecialChar *>(inset)->kind()
2348 == InsetSpecialChar::HYPHENATION
2350 cursor.pos(cursor.pos() + 1);
2352 // Finally, we copy the word to a string and return it
2354 if (selection.cursor.pos() < cursor.pos()) {
2355 Paragraph::size_type i;
2356 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2357 if (cursor.par()->getChar(i) != Paragraph::META_INSET)
2358 str += cursor.par()->getChar(i);
2365 // This one is also only for the spellchecker
2366 void LyXText::selectSelectedWord(BufferView * bview)
2368 if (the_locking_inset) {
2369 the_locking_inset->selectSelectedWord(bview);
2372 // move cursor to the beginning
2373 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2375 // set the sel cursor
2376 selection.cursor = cursor;
2379 // now find the end of the word
2380 while (cursor.pos() < cursor.par()->size()
2381 && (cursor.par()->isLetter(cursor.pos())
2382 // assignment is intentional here
2383 || ((inset = getInset())
2384 && inset->lyxCode() == Inset::SPECIALCHAR_CODE
2385 && static_cast<InsetSpecialChar *>(inset)->kind()
2386 == InsetSpecialChar::HYPHENATION
2388 cursor.pos(cursor.pos() + 1);
2390 setCursor(bview, cursor.par(), cursor.pos());
2392 // finally set the selection
2393 setSelection(bview);
2397 /* -------> Delete from cursor up to the end of the current or next word. */
2398 void LyXText::deleteWordForward(BufferView * bview)
2400 if (!cursor.par()->size())
2403 LyXCursor tmpcursor = cursor;
2404 tmpcursor.row(0); //Â ??
2405 selection.set(true); // to avoid deletion
2406 cursorRightOneWord(bview);
2407 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2408 selection.cursor = cursor;
2410 setSelection(bview);
2412 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2413 cutSelection(bview, true, false);
2418 /* -------> Delete from cursor to start of current or prior word. */
2419 void LyXText::deleteWordBackward(BufferView * bview)
2421 if (!cursor.par()->size())
2424 LyXCursor tmpcursor = cursor;
2425 tmpcursor.row(0); // ??
2426 selection.set(true); // to avoid deletion
2427 cursorLeftOneWord(bview);
2428 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2429 selection.cursor = cursor;
2431 setSelection(bview);
2432 cutSelection(bview, true, false);
2437 /* -------> Kill to end of line. */
2438 void LyXText::deleteLineForward(BufferView * bview)
2440 if (!cursor.par()->size())
2441 // Paragraph is empty, so we just go to the right
2444 LyXCursor tmpcursor = cursor;
2445 // We can't store the row over a regular setCursor
2446 // so we set it to 0 and reset it afterwards.
2447 tmpcursor.row(0); //Â ??
2448 selection.set(true); // to avoid deletion
2450 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2451 selection.cursor = cursor;
2453 setSelection(bview);
2454 // What is this test for ??? (JMarc)
2455 if (!selection.set()) {
2456 deleteWordForward(bview);
2458 cutSelection(bview, true, false);
2464 // Change the case of a word at cursor position.
2465 // This function directly manipulates Paragraph::text because there
2466 // is no Paragraph::SetChar currently. I did what I could to ensure
2467 // that it is correct. I guess part of it should be moved to
2468 // Paragraph, but it will have to change for 1.1 anyway. At least
2469 // it does not access outside of the allocated array as the older
2470 // version did. (JMarc)
2471 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2476 if (selection.set()) {
2477 from = selection.start;
2480 getWord(from, to, PARTIAL_WORD);
2481 setCursor(bview, to.par(), to.pos() + 1);
2484 changeRegionCase(bview, from, to, action);
2488 void LyXText::changeRegionCase(BufferView * bview,
2489 LyXCursor const & from,
2490 LyXCursor const & to,
2491 LyXText::TextCase action)
2493 lyx::Assert(from <= to);
2495 setUndo(bview, Undo::FINISH,
2496 from.par(), to.par()->next());
2498 Paragraph::size_type pos = from.pos();
2499 Paragraph * par = from.par();
2501 while (par && (pos != to.pos() || par != to.par())) {
2502 unsigned char c = par->getChar(pos);
2503 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2505 case text_lowercase:
2508 case text_capitalization:
2510 action = text_lowercase;
2512 case text_uppercase:
2517 par->setChar(pos, c);
2518 checkParagraph(bview, par, pos);
2521 if (pos == par->size()) {
2526 if (to.row() != from.row()) {
2527 refresh_y = from.y() - from.row()->baseline();
2528 refresh_row = from.row();
2529 status(bview, LyXText::NEED_MORE_REFRESH);
2534 void LyXText::transposeChars(BufferView & bview)
2536 Paragraph * tmppar = cursor.par();
2538 setUndo(&bview, Undo::FINISH,
2539 tmppar, tmppar->next());
2541 Paragraph::size_type tmppos = cursor.pos();
2543 // First decide if it is possible to transpose at all
2545 // We are at the beginning of a paragraph.
2546 if (tmppos == 0) return;
2548 // We are at the end of a paragraph.
2549 if (tmppos == tmppar->size() - 1) return;
2551 unsigned char c1 = tmppar->getChar(tmppos);
2552 unsigned char c2 = tmppar->getChar(tmppos - 1);
2554 if (c1 != Paragraph::META_INSET
2555 && c2 != Paragraph::META_INSET) {
2556 tmppar->setChar(tmppos, c2);
2557 tmppar->setChar(tmppos - 1, c1);
2559 // We should have an implementation that handles insets
2560 // as well, but that will have to come later. (Lgb)
2561 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2565 void LyXText::Delete(BufferView * bview)
2567 // this is a very easy implementation
2569 LyXCursor old_cursor = cursor;
2570 int const old_cur_par_id = old_cursor.par()->id();
2571 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2572 old_cursor.par()->previous()->id() : 0;
2574 // just move to the right
2577 // CHECK Look at the comment here.
2578 // This check is not very good...
2579 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2580 // and that can very well delete the par or par->previous in
2581 // old_cursor. Will a solution where we compare paragraph id's
2583 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2584 == old_cur_par_prev_id
2585 && cursor.par()->id() != old_cur_par_id)
2587 return; // delete-empty-paragraph-mechanism has done it
2590 // if you had success make a backspace
2591 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2592 LyXCursor tmpcursor = cursor;
2593 cursor = old_cursor; // to make sure undo gets the right cursor position
2594 setUndo(bview, Undo::DELETE,
2595 cursor.par(), cursor.par()->next());
2602 void LyXText::backspace(BufferView * bview)
2604 // Get the font that is used to calculate the baselineskip
2605 Paragraph::size_type lastpos = cursor.par()->size();
2606 LyXFont rawparfont =
2607 cursor.par()->getFontSettings(bview->buffer()->params,
2610 if (cursor.pos() == 0) {
2611 // The cursor is at the beginning of a paragraph,
2612 // so the the backspace will collapse two paragraphs into one.
2614 // we may paste some paragraphs
2616 // is it an empty paragraph?
2619 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2620 // This is an empty paragraph and we delete it just by moving the cursor one step
2621 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2622 // of the paragraph.
2624 if (cursor.par()->previous()) {
2625 Paragraph * tmppar = cursor.par()->previous();
2626 if (cursor.par()->getLayout() == tmppar->getLayout()
2627 && cursor.par()->getAlign() == tmppar->getAlign()) {
2628 // Inherit bottom DTD from the paragraph below.
2629 // (the one we are deleting)
2630 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2631 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2632 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2637 // the layout things can change the height of a row !
2638 int const tmpheight = cursor.row()->height();
2639 setHeightOfRow(bview, cursor.row());
2640 if (cursor.row()->height() != tmpheight) {
2641 refresh_y = cursor.y() - cursor.row()->baseline();
2642 refresh_row = cursor.row();
2643 status(bview, LyXText::NEED_MORE_REFRESH);
2649 if (cursor.par()->previous()) {
2650 setUndo(bview, Undo::DELETE,
2651 cursor.par()->previous(), cursor.par()->next());
2654 Paragraph * tmppar = cursor.par();
2655 Row * tmprow = cursor.row();
2657 // We used to do cursorLeftIntern() here, but it is
2658 // not a good idea since it triggers the auto-delete
2659 // mechanism. So we do a cursorLeftIntern()-lite,
2660 // without the dreaded mechanism. (JMarc)
2661 if (cursor.par()->previous()) {
2662 // steps into the above paragraph.
2663 setCursorIntern(bview, cursor.par()->previous(),
2664 cursor.par()->previous()->size(),
2668 /* Pasting is not allowed, if the paragraphs have different
2669 layout. I think it is a real bug of all other
2670 word processors to allow it. It confuses the user.
2671 Even so with a footnote paragraph and a non-footnote
2672 paragraph. I will not allow pasting in this case,
2673 because the user would be confused if the footnote behaves
2674 different wether it is open or closed.
2676 Correction: Pasting is always allowed with standard-layout
2678 if (cursor.par() != tmppar
2679 && (cursor.par()->getLayout() == tmppar->getLayout()
2680 || tmppar->getLayout() == 0 /*standard*/)
2681 && cursor.par()->getAlign() == tmppar->getAlign())
2683 removeParagraph(tmprow);
2685 cursor.par()->pasteParagraph(bview->buffer()->params);
2687 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2688 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2689 // strangely enough it seems that commenting out the line above removes
2690 // most or all of the segfaults. I will however also try to move the
2691 // two Remove... lines in front of the PasteParagraph too.
2694 cursor.pos(cursor.pos() - 1);
2696 status(bview, LyXText::NEED_MORE_REFRESH);
2697 refresh_row = cursor.row();
2698 refresh_y = cursor.y() - cursor.row()->baseline();
2700 // remove the lost paragraph
2701 // This one is not safe, since the paragraph that the tmprow and the
2702 // following rows belong to has been deleted by the PasteParagraph
2703 // above. The question is... could this be moved in front of the
2705 //RemoveParagraph(tmprow);
2706 //RemoveRow(tmprow);
2708 // This rebuilds the rows.
2709 appendParagraph(bview, cursor.row());
2710 updateCounters(bview, cursor.row());
2712 // the row may have changed, block, hfills etc.
2713 setCursor(bview, cursor.par(), cursor.pos(), false);
2716 /* this is the code for a normal backspace, not pasting
2718 setUndo(bview, Undo::DELETE,
2719 cursor.par(), cursor.par()->next());
2720 // We used to do cursorLeftIntern() here, but it is
2721 // not a good idea since it triggers the auto-delete
2722 // mechanism. So we do a cursorLeftIntern()-lite,
2723 // without the dreaded mechanism. (JMarc)
2724 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2725 false, cursor.boundary());
2727 // some insets are undeletable here
2728 if (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
2729 if (!cursor.par()->getInset(cursor.pos())->deletable())
2731 // force complete redo when erasing display insets
2732 // this is a cruel method but safe..... Matthias
2733 if (cursor.par()->getInset(cursor.pos())->display() ||
2734 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2735 cursor.par()->erase(cursor.pos());
2736 redoParagraph(bview);
2741 Row * row = cursor.row();
2742 int y = cursor.y() - row->baseline();
2743 Paragraph::size_type z;
2744 /* remember that a space at the end of a row doesnt count
2745 * when calculating the fill */
2746 if (cursor.pos() < rowLast(row) ||
2747 !cursor.par()->isLineSeparator(cursor.pos())) {
2748 row->fill(row->fill() + singleWidth(bview,
2753 /* some special code when deleting a newline. This is similar
2754 * to the behavior when pasting paragraphs */
2755 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2756 cursor.par()->erase(cursor.pos());
2757 // refresh the positions
2759 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2760 tmprow = tmprow->next();
2761 tmprow->pos(tmprow->pos() - 1);
2763 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2764 cursor.pos(cursor.pos() - 1);
2766 if (cursor.pos() < cursor.par()->size()
2767 && !cursor.par()->isSeparator(cursor.pos())) {
2768 cursor.par()->insertChar(cursor.pos(), ' ');
2769 setCharFont(bview->buffer(), cursor.par(),
2770 cursor.pos(), current_font);
2771 // refresh the positions
2773 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2774 tmprow = tmprow->next();
2775 tmprow->pos(tmprow->pos() + 1);
2779 cursor.par()->erase(cursor.pos());
2781 // refresh the positions
2783 while (tmprow->next()
2784 && tmprow->next()->par() == row->par()) {
2785 tmprow = tmprow->next();
2786 tmprow->pos(tmprow->pos() - 1);
2789 // delete newlines at the beginning of paragraphs
2790 while (cursor.par()->size() &&
2791 cursor.par()->isNewline(cursor.pos()) &&
2792 cursor.pos() == beginningOfMainBody(bview->buffer(),
2794 cursor.par()->erase(cursor.pos());
2795 // refresh the positions
2797 while (tmprow->next() &&
2798 tmprow->next()->par() == row->par()) {
2799 tmprow = tmprow->next();
2800 tmprow->pos(tmprow->pos() - 1);
2805 // is there a break one row above
2806 if (row->previous() && row->previous()->par() == row->par()) {
2807 z = nextBreakPoint(bview, row->previous(),
2809 if (z >= row->pos()) {
2812 Row * tmprow = row->previous();
2814 // maybe the current row is now empty
2815 if (row->pos() >= row->par()->size()) {
2820 breakAgainOneRow(bview, row);
2821 if (row->next() && row->next()->par() == row->par())
2822 need_break_row = row->next();
2827 // set the dimensions of the row above
2828 y -= tmprow->height();
2829 tmprow->fill(fill(bview, tmprow,
2831 setHeightOfRow(bview, tmprow);
2834 refresh_row = tmprow;
2835 status(bview, LyXText::NEED_MORE_REFRESH);
2836 setCursor(bview, cursor.par(), cursor.pos(),
2837 false, cursor.boundary());
2838 //current_font = rawtmpfont;
2839 //real_current_font = realtmpfont;
2840 // check, whether the last character's font has changed.
2842 cursor.par()->getFontSettings(bview->buffer()->params,
2843 cursor.par()->size() - 1))
2844 redoHeightOfParagraph(bview, cursor);
2849 // break the cursor row again
2850 if (row->next() && row->next()->par() == row->par() &&
2851 (rowLast(row) == row->par()->size() - 1 ||
2852 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2854 /* it can happen that a paragraph loses one row
2855 * without a real breakup. This is when a word
2856 * is to long to be broken. Well, I don t care this
2858 if (rowLast(row) == row->par()->size() - 1)
2859 removeRow(row->next());
2863 status(bview, LyXText::NEED_MORE_REFRESH);
2865 breakAgainOneRow(bview, row);
2866 // will the cursor be in another row now?
2867 if (row->next() && row->next()->par() == row->par() &&
2868 rowLast(row) <= cursor.pos()) {
2870 breakAgainOneRow(bview, row);
2873 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2875 if (row->next() && row->next()->par() == row->par())
2876 need_break_row = row->next();
2880 // set the dimensions of the row
2881 row->fill(fill(bview, row, workWidth(bview)));
2882 int const tmpheight = row->height();
2883 setHeightOfRow(bview, row);
2884 if (tmpheight == row->height())
2885 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2887 status(bview, LyXText::NEED_MORE_REFRESH);
2890 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2894 // current_font = rawtmpfont;
2895 // real_current_font = realtmpfont;
2897 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2898 != cursor.boundary())
2899 setCursor(bview, cursor.par(), cursor.pos(), false,
2900 !cursor.boundary());
2902 lastpos = cursor.par()->size();
2903 if (cursor.pos() == lastpos)
2904 setCurrentFont(bview);
2906 // check, whether the last characters font has changed.
2908 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2909 redoHeightOfParagraph(bview, cursor);
2911 // now the special right address boxes
2912 if (textclasslist.Style(bview->buffer()->params.textclass,
2913 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2914 redoDrawingOfParagraph(bview, cursor);
2920 void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
2921 Row * row_ptr, int y, bool cleared)
2923 // returns a printed row
2924 Painter & pain = bview->painter();
2927 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
2929 Paragraph::size_type const last = rowLastPrintable(row_ptr);
2931 Paragraph::size_type vpos;
2932 Paragraph::size_type pos;
2936 LyXFont font(LyXFont::ALL_SANE);
2938 if (row_ptr->height() <= 0) {
2939 lyxerr << "LYX_ERROR: row.height: "
2940 << row_ptr->height() << endl;
2945 float fill_separator;
2947 float fill_label_hfill;
2948 prepareToPrint(bview, row_ptr, x, fill_separator,
2949 fill_hfill, fill_label_hfill);
2951 if (inset_owner && (x < 0))
2955 // clear the area where we want to paint/print
2956 int const ww = bview->workWidth();
2958 bool clear_area = true;
2961 if (!bview->screen()->forceClear() && last == row_ptr->pos()
2962 && row_ptr->par()->getChar(row_ptr->pos()) == Paragraph::META_INSET
2963 && (inset = row_ptr->par()->getInset(row_ptr->pos()))) {
2964 clear_area = inset->doClearArea();
2966 // we don't need to clear it's already done!!!
2969 } else if (clear_area) {
2970 int const y = y_offset < 0 ? 0 : y_offset;
2971 int const h = y_offset < 0 ?
2972 row_ptr->height() + y_offset : row_ptr->height();
2973 int const w = inset_owner ?
2974 inset_owner->textWidth(bview, true) : ww;
2975 int const x = x_offset;
2976 pain.fillRectangle(x, y, w, h, backgroundColor());
2977 } else if (inset != 0) {
2978 int h = row_ptr->baseline() - inset->ascent(bview, font);
2979 // first clear the whole row above the inset!
2981 int const w = (inset_owner ?
2982 inset_owner->textWidth(bview, true) : ww);
2983 pain.fillRectangle(x_offset, y_offset, w, h,
2986 h += inset->ascent(bview, font) + inset->descent(bview, font);
2987 // clear the space below the inset!
2988 if ((row_ptr->height() - h) > 0) {
2989 int const w = (inset_owner ?
2990 inset_owner->textWidth(bview, true) : ww);
2991 pain.fillRectangle(x_offset, y_offset + h,
2992 w, row_ptr->height() - h,
2995 // clear the space behind the inset, if needed
2996 if (!inset->display() && !inset->needFullRow()) {
2997 int const w = (inset_owner ?
2998 inset_owner->textWidth(bview, true) : ww);
2999 int const xp = int(x) + inset->width(bview, font);
3001 pain.fillRectangle(xp, y_offset,
3002 w-xp, row_ptr->height(),
3008 if (selection.set()) {
3009 int const w = (inset_owner ?
3010 inset_owner->textWidth(bview, true) : ww);
3012 if (bidi_same_direction) {
3013 if (selection.start.row() == row_ptr &&
3014 selection.end.row() == row_ptr) {
3015 if (selection.start.x() < selection.end.x())
3016 pain.fillRectangle(x_offset + selection.start.x(),
3018 selection.end.x() - selection.start.x(),
3022 pain.fillRectangle(x_offset + selection.end.x(),
3024 selection.start.x() - selection.end.x(),
3027 } else if (selection.start.row() == row_ptr) {
3029 pain.fillRectangle(x_offset, y_offset,
3030 selection.start.x(),
3034 pain.fillRectangle(x_offset + selection.start.x(),
3036 w - selection.start.x(),
3039 } else if (selection.end.row() == row_ptr) {
3041 pain.fillRectangle(x_offset + selection.end.x(),
3043 w - selection.end.x(),
3047 pain.fillRectangle(x_offset, y_offset,
3051 } else if (y > selection.start.y()
3052 && y < selection.end.y()) {
3053 pain.fillRectangle(x_offset, y_offset, w,
3057 } else if (selection.start.row() != row_ptr &&
3058 selection.end.row() != row_ptr &&
3059 y > selection.start.y()
3060 && y < selection.end.y()) {
3061 pain.fillRectangle(x_offset, y_offset, w,
3064 } else if (selection.start.row() == row_ptr ||
3065 selection.end.row() == row_ptr) {
3067 if ((selection.start.row() != row_ptr && !is_rtl) ||
3068 (selection.end.row() != row_ptr && is_rtl))
3069 pain.fillRectangle(x_offset, y_offset,
3073 Paragraph::size_type main_body =
3074 beginningOfMainBody(bview->buffer(),
3077 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3078 pos = vis2log(vpos);
3079 float const old_tmpx = tmpx;
3080 if (main_body > 0 && pos == main_body-1) {
3081 tmpx += fill_label_hfill +
3082 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3083 row_ptr->par()->getLayout()).labelsep,
3084 getLabelFont(bview->buffer(),row_ptr->par()));
3085 if (row_ptr->par()->isLineSeparator(main_body-1))
3086 tmpx -= singleWidth(bview, row_ptr->par(), main_body-1);
3088 if (hfillExpansion(bview->buffer(), row_ptr, pos)) {
3089 tmpx += singleWidth(bview, row_ptr->par(), pos);
3090 if (pos >= main_body)
3093 tmpx += fill_label_hfill;
3095 else if (row_ptr->par()->isSeparator(pos)) {
3096 tmpx += singleWidth(bview, row_ptr->par(), pos);
3097 if (pos >= main_body)
3098 tmpx += fill_separator;
3100 tmpx += singleWidth(bview, row_ptr->par(), pos);
3102 if ((selection.start.row() != row_ptr ||
3103 selection.start.pos() <= pos) &&
3104 (selection.end.row() != row_ptr ||
3105 pos < selection.end.pos()) )
3106 // Here we do not use x_offset as x_offset was
3108 pain.fillRectangle(int(old_tmpx),
3110 int(tmpx - old_tmpx + 1),
3115 if ((selection.start.row() != row_ptr && is_rtl) ||
3116 (selection.end.row() != row_ptr && !is_rtl) )
3117 pain.fillRectangle(x_offset + int(tmpx),
3127 // Draw appendix lines
3128 Paragraph * firstpar = row_ptr->par();
3130 if (firstpar->params().appendix()) {
3131 pain.line(1, y_offset,
3132 1, y_offset + row_ptr->height(),
3133 LColor::appendixline);
3134 pain.line(ww - 2, y_offset,
3135 ww - 2, y_offset + row_ptr->height(),
3136 LColor::appendixline);
3140 Paragraph::depth_type const depth = firstpar->getDepth();
3142 Paragraph::depth_type next_depth = 0;
3143 Paragraph::depth_type prev_depth = 0;
3144 if (row_ptr->next())
3145 next_depth = row_ptr->next()->par()->getDepth();
3146 if (row_ptr->previous())
3147 prev_depth = row_ptr->previous()->par()->getDepth();
3149 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3150 int const line_x = (LYX_PAPER_MARGIN / 5) *
3151 i + box_x + x_offset;
3152 pain.line(line_x, y_offset, line_x,
3153 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3157 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3160 pain.fillRectangle(line_x,
3161 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3162 LYX_PAPER_MARGIN / 5, 2,
3168 LyXLayout const & layout =
3169 textclasslist.Style(bview->buffer()->params.textclass,
3170 row_ptr->par()->getLayout());
3173 int y_bottom = row_ptr->height();
3175 // is it a first row?
3176 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3178 // start of appendix?
3179 if (row_ptr->par()->params().startOfAppendix()) {
3180 pain.line(1, y_offset,
3182 LColor::appendixline);
3185 // think about the margins
3186 if (!row_ptr->previous() && bv_owner)
3187 y_top += LYX_PAPER_MARGIN;
3189 // draw a top pagebreak
3190 if (row_ptr->par()->params().pagebreakTop()) {
3192 pb_font.setColor(LColor::pagebreak).decSize();
3196 pain.line(0, y_offset + y_top + 2*defaultHeight(),
3198 y_offset + y_top + 2 * defaultHeight(),
3200 Painter::line_onoffdash);
3201 lyxfont::rectText(_("Page Break (top)"), pb_font,
3203 pain.rectText((ww - w)/2,
3204 y_offset + y_top + 2 * defaultHeight() + d,
3205 _("Page Break (top)"),
3209 y_top += 3 * defaultHeight();
3212 if (row_ptr->par()->params().spaceTop().kind() == VSpace::VFILL) {
3214 pain.line(0, y_offset + 2 + y_top,
3215 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3218 pain.line(0, y_offset + y_top + 3 * defaultHeight(),
3220 y_offset + y_top + 3 * defaultHeight(),
3223 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3224 LYX_PAPER_MARGIN / 2,
3225 y_offset + y_top + 3 * defaultHeight(),
3228 y_top += 3 * defaultHeight();
3231 // think about user added space
3232 y_top += int(row_ptr->par()->params().spaceTop().inPixels(bview));
3234 // think about the parskip
3235 // some parskips VERY EASY IMPLEMENTATION
3236 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3237 if (layout.latextype == LATEX_PARAGRAPH
3238 && firstpar->getDepth() == 0
3239 && firstpar->previous())
3240 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3241 else if (firstpar->previous()
3242 && textclasslist.Style(bview->buffer()->params.textclass,
3243 firstpar->previous()->getLayout()).latextype == LATEX_PARAGRAPH
3244 && firstpar->previous()->getDepth() == 0)
3245 // is it right to use defskip here, too? (AS)
3246 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3249 if (row_ptr->par()->params().lineTop()) {
3251 y_top += lyxfont::ascent('x',
3252 getFont(bview->buffer(),
3253 row_ptr->par(), 0));
3254 int const w = (inset_owner ?
3255 inset_owner->width(bview, font) : ww);
3256 int const xp = static_cast<int>(inset_owner ? x : 0);
3257 pain.line(xp, y_offset + y_top,
3258 w, y_offset + y_top,
3260 Painter::line_solid,
3261 Painter::line_thick);
3263 y_top += lyxfont::ascent('x',getFont(bview->buffer(),
3264 row_ptr->par(), 0));
3267 // should we print a label?
3268 if (layout.labeltype >= LABEL_STATIC
3269 && (layout.labeltype != LABEL_STATIC
3270 || layout.latextype != LATEX_ENVIRONMENT
3271 || row_ptr->par()->isFirstInSequence())) {
3272 font = getLabelFont(bview->buffer(), row_ptr->par());
3273 if (!row_ptr->par()->getLabelstring().empty()) {
3275 string const tmpstring =
3276 row_ptr->par()->getLabelstring();
3278 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3279 if (bview->buffer()->params.secnumdepth >= 0) {
3280 // this is special code for
3281 // the chapter layout. This is
3282 // printed in an extra row
3283 // and has a pagebreak at
3285 float spacing_val = 1.0;
3286 if (!row_ptr->par()->params().spacing().isDefault()) {
3287 spacing_val = row_ptr->par()->params().spacing().getValue();
3289 spacing_val = bview->buffer()->params.spacing.getValue();
3292 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3293 + int(layout.parsep) * defaultHeight();
3295 tmpx = ww - leftMargin(bview, row_ptr) -
3296 lyxfont::width(tmpstring, font);
3297 pain.text(int(tmpx),
3298 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3303 tmpx = ww - leftMargin(bview, row_ptr)
3304 + lyxfont::width(layout.labelsep, font);
3306 tmpx = x - lyxfont::width(layout.labelsep, font)
3307 - lyxfont::width(tmpstring, font);
3310 pain.text(int(tmpx),
3311 y_offset + row_ptr->baseline(),
3315 // the labels at the top of an environment.
3316 // More or less for bibliography
3317 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3318 layout.labeltype == LABEL_BIBLIO ||
3319 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3320 if (row_ptr->par()->isFirstInSequence()) {
3321 font = getLabelFont(bview->buffer(),
3323 if (!row_ptr->par()->getLabelstring().empty()) {
3324 string const tmpstring =
3325 row_ptr->par()->getLabelstring();
3326 float spacing_val = 1.0;
3327 if (!row_ptr->par()->params().spacing().isDefault()) {
3328 spacing_val = row_ptr->par()->params().spacing().getValue();
3330 spacing_val = bview->buffer()->params.spacing.getValue();
3333 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3334 + (layout.labelbottomsep * defaultHeight()));
3337 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3338 tmpx = ( (is_rtl ? leftMargin(bview, row_ptr) : x)
3339 + ww - rightMargin(bview->buffer(), row_ptr) ) / 2;
3340 tmpx -= lyxfont::width(tmpstring, font) / 2;
3342 tmpx = ww - leftMargin(bview, row_ptr) -
3343 lyxfont::width(tmpstring, font);
3344 pain.text(int(tmpx),
3345 y_offset + row_ptr->baseline()
3346 - row_ptr->ascent_of_text()
3352 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3353 font = getLayoutFont(bview->buffer(), row_ptr->par());
3355 tmpx = ww - leftMargin(bview, row_ptr)
3356 + lyxfont::width(layout.labelsep, font);
3358 tmpx = x - lyxfont::width(layout.labelsep, font)
3359 - row_ptr->par()->bibkey->width(bview, font);
3360 row_ptr->par()->bibkey->draw(bview, font,
3361 y_offset + row_ptr->baseline(),
3366 // is it a last row?
3367 Paragraph * par = row_ptr->par();
3368 if (row_ptr->par() == par
3369 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
3370 // think about the margins
3371 if (!row_ptr->next() && bv_owner)
3372 y_bottom -= LYX_PAPER_MARGIN;
3374 // draw a bottom pagebreak
3375 if (firstpar->params().pagebreakBottom()) {
3377 pb_font.setColor(LColor::pagebreak).decSize();
3378 int const y_place = y_offset + y_bottom
3379 - 2 * defaultHeight();
3385 .line(0, y_place, ww, y_place,
3387 Painter::line_onoffdash);
3388 lyxfont::rectText(_("Page Break (bottom)"), pb_font,
3390 pain.rectText((ww - w) / 2, y_place + d,
3391 _("Page Break (bottom)"),
3395 y_bottom -= 3 * defaultHeight();
3398 if (firstpar->params().spaceBottom().kind() == VSpace::VFILL) {
3399 // draw a vfill bottom
3400 int const y_place = y_offset + y_bottom
3401 - 3 * defaultHeight();
3403 pain.line(0, y_place,
3404 LYX_PAPER_MARGIN, y_place,
3406 pain.line(0, y_offset + y_bottom - 2,
3408 y_offset + y_bottom - 2,
3410 pain.line(LYX_PAPER_MARGIN / 2,
3412 LYX_PAPER_MARGIN / 2,
3413 y_offset + y_bottom - 2,
3415 y_bottom -= 3 * defaultHeight();
3418 // think about user added space
3419 y_bottom -= int(firstpar->params().spaceBottom().inPixels(bview));
3421 if (firstpar->params().lineBottom()) {
3422 // draw a bottom line
3423 y_bottom -= lyxfont::ascent('x',
3424 getFont(bview->buffer(),
3427 int const w = (inset_owner ?
3428 inset_owner->width(bview, font) : ww);
3429 int const xp = static_cast<int>(inset_owner ? x : 0);
3430 pain.line(xp, y_offset + y_bottom,
3431 w, y_offset + y_bottom,
3432 LColor::topline, Painter::line_solid,
3433 Painter::line_thick);
3434 y_bottom -= lyxfont::ascent('x',
3435 getFont(bview->buffer(),
3441 int const endlabel =
3442 row_ptr->par()->getEndLabel(bview->buffer()->params);
3445 case END_LABEL_FILLED_BOX:
3447 LyXFont const font = getFont(bview->buffer(),
3448 row_ptr->par(), last);
3449 int const size = int(0.75 * lyxfont::maxAscent(font));
3450 int const y = (y_offset + row_ptr->baseline()) - size;
3451 int x = is_rtl ? LYX_PAPER_MARGIN
3452 : ww - LYX_PAPER_MARGIN - size;
3454 if (row_ptr->fill() <= size)
3455 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3456 if (endlabel == END_LABEL_BOX) {
3457 pain.line(x, y, x, y + size,
3459 pain.line(x + size, y, x + size , y + size,
3461 pain.line(x, y, x + size, y,
3463 pain.line(x, y + size, x + size, y + size,
3466 pain.fillRectangle(x, y, size, size,
3470 case END_LABEL_STATIC:
3472 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->getLayout();
3473 string const tmpstring = textclasslist.
3474 Style(bview->buffer()->params.textclass,
3475 layout).endlabelstring();
3476 font = getLabelFont(bview->buffer(), row_ptr->par());
3477 int const tmpx = is_rtl ?
3478 int(x) - lyxfont::width(tmpstring, font)
3479 : ww - rightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
3480 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
3483 case END_LABEL_NO_LABEL:
3488 // draw the text in the pixmap
3490 vpos = row_ptr->pos();
3492 Paragraph::size_type main_body =
3493 beginningOfMainBody(bview->buffer(), row_ptr->par());
3494 if (main_body > 0 &&
3495 (main_body-1 > last ||
3496 !row_ptr->par()->isLineSeparator(main_body - 1)))
3499 while (vpos <= last) {
3500 pos = vis2log(vpos);
3501 if (main_body > 0 && pos == main_body - 1) {
3502 x += fill_label_hfill
3503 + lyxfont::width(layout.labelsep,
3504 getLabelFont(bview->buffer(),
3506 - singleWidth(bview,
3511 if (row_ptr->par() ->isHfill(pos)) {
3514 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3516 y_offset + row_ptr->baseline(),
3519 if (hfillExpansion(bview->buffer(),
3521 if (pos >= main_body) {
3523 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3524 int(x + fill_hfill),
3525 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3527 Painter::line_onoffdash);
3531 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3532 int(x + fill_label_hfill),
3533 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3535 Painter::line_onoffdash);
3537 x += fill_label_hfill;
3540 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3542 y_offset + row_ptr->baseline(),
3547 } else if (row_ptr->par()->isSeparator(pos)) {
3548 x += singleWidth(bview,
3549 row_ptr->par(), pos);
3550 if (pos >= main_body)
3551 x += fill_separator;
3554 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
3559 int LyXText::defaultHeight() const
3561 LyXFont font(LyXFont::ALL_SANE);
3562 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3566 /* returns the column near the specified x-coordinate of the row
3567 * x is set to the real beginning of this column */
3568 int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3569 bool & boundary) const
3572 float fill_separator;
3574 float fill_label_hfill;
3576 prepareToPrint(bview, row, tmpx, fill_separator,
3577 fill_hfill, fill_label_hfill);
3579 Paragraph::size_type vc = row->pos();
3580 Paragraph::size_type last = rowLastPrintable(row);
3581 Paragraph::size_type c = 0;
3582 LyXLayout const & layout =
3583 textclasslist.Style(bview->buffer()->params.textclass,
3584 row->par()->getLayout());
3585 bool left_side = false;
3587 Paragraph::size_type
3588 main_body = beginningOfMainBody(bview->buffer(), row->par());
3589 float last_tmpx = tmpx;
3591 if (main_body > 0 &&
3592 (main_body - 1 > last ||
3593 !row->par()->isLineSeparator(main_body - 1)))
3596 while (vc <= last && tmpx <= x) {
3599 if (main_body > 0 && c == main_body-1) {
3600 tmpx += fill_label_hfill +
3601 lyxfont::width(layout.labelsep,
3602 getLabelFont(bview->buffer(), row->par()));
3603 if (row->par()->isLineSeparator(main_body - 1))
3604 tmpx -= singleWidth(bview, row->par(), main_body-1);
3607 if (hfillExpansion(bview->buffer(), row, c)) {
3608 x += singleWidth(bview, row->par(), c);
3612 tmpx += fill_label_hfill;
3614 else if (row->par()->isSeparator(c)) {
3615 tmpx += singleWidth(bview, row->par(), c);
3617 tmpx+= fill_separator;
3619 tmpx += singleWidth(bview, row->par(), c);
3623 if ((tmpx + last_tmpx) / 2 > x) {
3628 if (vc > last + 1) // This shouldn't happen.
3632 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3633 // some speedup if rtl_support=false
3634 && (!row->next() || row->next()->par() != row->par());
3635 bool const rtl = (lastrow)
3636 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3637 : false; // If lastrow is false, we don't need to compute
3638 // the value of rtl.
3640 if (row->pos() > last) // Row is empty?
3643 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3644 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3646 else if (vc == row->pos()) {
3648 if (bidi_level(c) % 2 == 1)
3651 c = vis2log(vc - 1);
3652 bool const rtl = (bidi_level(c) % 2 == 1);
3653 if (left_side == rtl) {
3655 boundary = isBoundary(bview->buffer(), row->par(), c);
3659 if (row->pos() <= last && c > last
3660 && row->par()->isNewline(last)) {
3661 if (bidi_level(last) % 2 == 0)
3662 tmpx -= singleWidth(bview, row->par(), last);
3664 tmpx += singleWidth(bview, row->par(), last);
3674 // returns pointer to a specified row
3675 Row * LyXText::getRow(Paragraph * par,
3676 Paragraph::size_type pos, int & y) const
3681 Row * tmprow = firstrow;
3684 // find the first row of the specified paragraph
3685 while (tmprow->next() && tmprow->par() != par) {
3686 y += tmprow->height();
3687 tmprow = tmprow->next();
3690 // now find the wanted row
3691 while (tmprow->pos() < pos
3693 && tmprow->next()->par() == par
3694 && tmprow->next()->pos() <= pos) {
3695 y += tmprow->height();
3696 tmprow = tmprow->next();