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 * ====================================================== */
15 #include "paragraph.h"
16 #include "lyx_gui_misc.h"
18 #include "bufferparams.h"
27 #include "lyxscreen.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
35 #include "insets/insetbib.h"
36 #include "insets/insettext.h"
38 #include "support/textutils.h"
39 #include "support/LAssert.h"
40 #include "support/lstrings.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::workWidth(BufferView * bview, Inset * inset) const
70 Paragraph::size_type pos = 0;
72 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
74 for (; it != bview->buffer()->inset_iterator_end(); ++it) {
82 return workWidth(bview);
85 LyXLayout const & layout =
86 textclasslist.Style(bview->buffer()->params.textclass,
89 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
90 // Optimization here: in most cases, the real row is
91 // not needed, but only the par/pos values. So we just
92 // construct a dummy row for leftMargin. (JMarc)
96 return workWidth(bview) - leftMargin(bview, &dummyrow);
99 Row * row = getRow(par, pos, dummy_y);
101 while(frow->previous() && frow->par() == frow->previous()->par())
102 frow = frow->previous();
103 unsigned int maxw = 0;
104 while(frow->next() && frow->par() == frow->next()->par()) {
105 if ((frow != row) && (maxw < frow->width()))
106 maxw = frow->width();
112 return workWidth(bview);
116 int LyXText::getRealCursorX(BufferView * bview) const
119 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
120 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
125 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
126 Paragraph::size_type pos) const
128 if (!Encodings::is_arabic(c))
129 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
130 return c + (0xb0 - '0');
134 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
135 unsigned char next_char = ' ';
137 for (Paragraph::size_type i = pos+1; i < par->size(); ++i)
138 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
139 next_char = par->getChar(i);
143 if (Encodings::is_arabic(next_char)) {
144 if (Encodings::is_arabic(prev_char))
145 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
147 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
149 if (Encodings::is_arabic(prev_char))
150 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
152 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
156 // This is the comments that some of the warnings below refers to.
157 // There are some issues in this file and I don't think they are
158 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
159 // this is a problem that has been here almost from day one and that a
160 // larger userbase with differenct access patters triggers the bad
161 // behaviour. (segfaults.) What I think happen is: In several places
162 // we store the paragraph in the current cursor and then moves the
163 // cursor. This movement of the cursor will delete paragraph at the
164 // old position if it is now empty. This will make the temporary
165 // pointer to the old cursor paragraph invalid and dangerous to use.
166 // And is some cases this will trigger a segfault. I have marked some
167 // of the cases where this happens with a warning, but I am sure there
168 // are others in this file and in text2.C. There is also a note in
169 // Delete() that you should read. In Delete I store the paragraph->id
170 // instead of a pointer to the paragraph. I am pretty sure this faulty
171 // use of temporary pointers to paragraphs that might have gotten
172 // invalidated (through a cursor movement) before they are used, are
173 // the cause of the strange crashes we get reported often.
175 // It is very tiresom to change this code, especially when it is as
176 // hard to read as it is. Help to fix all the cases where this is done
177 // would be greately appreciated.
181 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
182 Paragraph::size_type pos) const
184 char const c = par->getChar(pos);
185 return singleWidth(bview, par, pos, c);
189 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
190 Paragraph::size_type pos, char c) const
192 LyXFont const font = getFont(bview->buffer(), par, pos);
194 // The most common case is handled first (Asger)
195 if (IsPrintable(c)) {
196 if (font.language()->RightToLeft()) {
197 if (font.language()->lang() == "arabic" &&
198 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
199 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
200 if (Encodings::IsComposeChar_arabic(c))
203 c = transformChar(c, par, pos);
204 else if (font.language()->lang() == "hebrew" &&
205 Encodings::IsComposeChar_hebrew(c))
208 return lyxfont::width(c, font);
210 } else if (IsHfillChar(c)) {
211 return 3; /* Because of the representation
212 * as vertical lines */
213 } else if (c == Paragraph::META_INSET) {
214 Inset * tmpinset = par->getInset(pos);
216 #if 0 // seems not to be needed, but ...
217 tmpinset->update(bview, font);
219 return tmpinset->width(bview, font);
223 } else if (IsSeparatorChar(c))
225 else if (IsNewlineChar(c))
227 return lyxfont::width(c, font);
231 // Returns the paragraph position of the last character in the specified row
232 Paragraph::size_type LyXText::rowLast(Row const * row) const
234 if (row->next() == 0)
235 return row->par()->size() - 1;
236 else if (row->next()->par() != row->par())
237 return row->par()->size() - 1;
239 return row->next()->pos() - 1;
243 Paragraph::size_type LyXText::rowLastPrintable(Row const * row) const
245 Paragraph::size_type const last = rowLast(row);
246 if (last >= row->pos()
248 && row->next()->par() == row->par()
249 && row->par()->isSeparator(last))
256 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
258 bidi_same_direction = true;
259 if (!lyxrc.rtl_support) {
264 bidi_start = row->pos();
265 bidi_end = rowLastPrintable(row);
267 if (bidi_start > bidi_end) {
272 if (bidi_end + 2 - bidi_start >
273 static_cast<Paragraph::size_type>(log2vis_list.size())) {
274 Paragraph::size_type new_size =
275 (bidi_end + 2 - bidi_start < 500) ?
276 500 : 2 * (bidi_end + 2 - bidi_start);
277 log2vis_list.resize(new_size);
278 vis2log_list.resize(new_size);
279 bidi_levels.resize(new_size);
282 vis2log_list[bidi_end + 1 - bidi_start] = -1;
283 log2vis_list[bidi_end + 1 - bidi_start] = -1;
285 Paragraph::size_type stack[2];
287 row->par()->getParLanguage(buf->params)->RightToLeft();
291 Paragraph::size_type const main_body =
292 beginningOfMainBody(buf, row->par());
294 for (Paragraph::size_type lpos = bidi_start;
295 lpos <= bidi_end; ++lpos) {
296 bool is_space = row->par()->isLineSeparator(lpos);
297 Paragraph::size_type const pos =
298 (is_space && lpos + 1 <= bidi_end &&
299 !row->par()->isLineSeparator(lpos + 1) &&
300 !row->par()->isNewline(lpos + 1))
302 LyXFont font = row->par()->getFontSettings(buf->params, pos);
303 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
304 font.number() == LyXFont::ON &&
305 row->par()->getFontSettings(buf->params, lpos - 1).number()
307 font = row->par()->getFontSettings(buf->params, lpos);
312 bool new_rtl = font.isVisibleRightToLeft();
313 bool new_rtl0 = font.isRightToLeft();
316 if (lpos == main_body - 1
317 && row->pos() < main_body - 1
319 new_level = (rtl_par) ? 1 : 0;
320 new_rtl = new_rtl0 = rtl_par;
322 new_level = (new_rtl) ? 1 : 2;
324 new_level = (rtl_par) ? 2 : 0;
326 if (is_space && new_level >= level) {
332 int new_level2 = new_level;
334 if (level == new_level && rtl0 != new_rtl0) {
336 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
337 } else if (level < new_level) {
338 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
339 if (new_level > rtl_par)
340 bidi_same_direction = false;
342 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
345 bidi_levels[lpos - bidi_start] = new_level;
347 while (level > new_level2) {
348 Paragraph::size_type old_lpos =
350 int delta = lpos - old_lpos - 1;
353 log2vis_list[lpos - bidi_start] += delta;
354 log2vis_list[old_lpos - bidi_start] += delta;
356 while (level < new_level)
357 stack[level++] = lpos;
361 Paragraph::size_type const old_lpos = stack[--level];
362 int delta = bidi_end - old_lpos;
365 log2vis_list[old_lpos - bidi_start] += delta;
368 Paragraph::size_type vpos = bidi_start - 1;
369 for (Paragraph::size_type lpos = bidi_start;
370 lpos <= bidi_end; ++lpos) {
371 vpos += log2vis_list[lpos - bidi_start];
372 vis2log_list[vpos - bidi_start] = lpos;
373 log2vis_list[lpos - bidi_start] = vpos;
378 // This method requires a previous call to ComputeBidiTables()
379 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
380 Paragraph::size_type pos) const
382 if (!lyxrc.rtl_support || pos == 0)
385 if (!bidi_InRange(pos - 1)) {
386 /// This can happen if pos is the first char of a row.
387 /// Returning false in this case is incorrect!
391 bool const rtl = bidi_level(pos - 1) % 2;
392 bool const rtl2 = bidi_InRange(pos)
393 ? bidi_level(pos) % 2
394 : par->isRightToLeftPar(buf->params);
399 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
400 Paragraph::size_type pos,
401 LyXFont const & font) const
403 if (!lyxrc.rtl_support)
404 return false; // This is just for speedup
406 bool const rtl = font.isVisibleRightToLeft();
407 bool const rtl2 = bidi_InRange(pos)
408 ? bidi_level(pos) % 2
409 : par->isRightToLeftPar(buf->params);
414 void LyXText::draw(BufferView * bview, Row const * row,
415 Paragraph::size_type & vpos,
416 int offset, float & x, bool cleared)
418 Painter & pain = bview->painter();
420 Paragraph::size_type pos = vis2log(vpos);
421 char c = row->par()->getChar(pos);
424 if (IsNewlineChar(c)) {
426 // Draw end-of-line marker
427 LyXFont const font = getFont(bview->buffer(), row->par(), pos);
428 int const wid = lyxfont::width('n', font);
429 int const asc = lyxfont::maxAscent(font);
430 int const y = offset + row->baseline();
434 if (bidi_level(pos) % 2 == 0) {
435 xp[0] = int(x + wid * 0.375);
436 yp[0] = int(y - 0.875 * asc * 0.75);
439 yp[1] = int(y - 0.500 * asc * 0.75);
441 xp[2] = int(x + wid * 0.375);
442 yp[2] = int(y - 0.125 * asc * 0.75);
444 pain.lines(xp, yp, 3, LColor::eolmarker);
447 yp[0] = int(y - 0.500 * asc * 0.75);
449 xp[1] = int(x + wid);
450 yp[1] = int(y - 0.500 * asc * 0.75);
452 xp[2] = int(x + wid);
453 yp[2] = int(y - asc * 0.75);
455 pain.lines(xp, yp, 3, LColor::eolmarker);
457 xp[0] = int(x + wid * 0.625);
458 yp[0] = int(y - 0.875 * asc * 0.75);
460 xp[1] = int(x + wid);
461 yp[1] = int(y - 0.500 * asc * 0.75);
463 xp[2] = int(x + wid * 0.625);
464 yp[2] = int(y - 0.125 * asc * 0.75);
466 pain.lines(xp, yp, 3, LColor::eolmarker);
468 xp[0] = int(x + wid);
469 yp[0] = int(y - 0.500 * asc * 0.75);
472 yp[1] = int(y - 0.500 * asc * 0.75);
475 yp[2] = int(y - asc * 0.75);
477 pain.lines(xp, yp, 3, LColor::eolmarker);
483 LyXFont font = getFont(bview->buffer(), row->par(), pos);
484 LyXFont font2 = font;
486 if (c == Paragraph::META_INSET) {
487 Inset * tmpinset = row->par()->getInset(pos);
489 tmpinset->update(bview, font, false);
490 tmpinset->draw(bview, font, offset+row->baseline(), x,
492 if (!need_break_row && !inset_owner &&
493 bview->text->status() == CHANGED_IN_DRAW)
495 if (row->previous() && row->previous()->par() == row->par())
496 breakAgainOneRow(bview, row->previous());
497 setCursor(bview, cursor.par(), cursor.pos());
498 need_break_row = const_cast<Row *>(row);
503 if (lyxrc.mark_foreign_language &&
504 font.language() != latex_language &&
505 font.language() != bview->buffer()->params.language) {
506 int const y = offset + row->height() - 1;
507 pain.line(int(tmpx), y, int(x), y, LColor::language);
513 // usual characters, no insets
515 // Collect character that we can draw in one command
517 // This is dirty, but fast. Notice that it will never be too small.
518 // For the record, I'll note that Microsoft Word has a limit
519 // of 768 here. We have none :-) (Asger)
520 // Ok. I am the first to admit that the use of std::string will be
521 // a tiny bit slower than using a POD char array. However, I claim
522 // that this slowdown is so small that it is close to inperceptive.
523 // So IMHO we should go with the easier and clearer implementation.
524 // And even if 1024 is a large number here it might overflow, string
525 // will only overflow if the machine is out of memory...
526 static string textstring;
530 Paragraph::size_type const last = rowLastPrintable(row);
532 if (font.language()->lang() == "hebrew") {
533 if (Encodings::IsComposeChar_hebrew(c)) {
534 int const width = lyxfont::width(c, font2);
536 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
537 c = row->par()->getChar(i);
538 if (!Encodings::IsComposeChar_hebrew(c)) {
539 if (IsPrintableNonspace(c)) {
544 dx = (c == 'ø' || c == 'ã') // dalet / resh
545 ? width2 - width : (width2 - width) / 2;
551 pain.text(int(x) + dx, offset + row->baseline(),
554 while (vpos <= last &&
555 (pos = vis2log(vpos)) >= 0
556 && IsPrintableNonspace(c = row->par()->getChar(pos))
557 && !Encodings::IsComposeChar_hebrew(c)
558 && font2 == getFont(bview->buffer(), row->par(), pos)) {
562 // Draw text and set the new x position
563 pain.text(int(x), offset + row->baseline(),
565 x += lyxfont::width(textstring, font);
567 } else if (font.language()->lang() == "arabic" &&
568 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
569 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
570 if (Encodings::IsComposeChar_arabic(c)) {
571 c = transformChar(c, row->par(), pos);
573 int const width = lyxfont::width(c, font2);
575 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
576 c = row->par()->getChar(i);
577 if (!Encodings::IsComposeChar_arabic(c)) {
578 if (IsPrintableNonspace(c)) {
583 dx = (width2 - width) / 2;
589 pain.text(int(x) + dx, offset + row->baseline(),
592 textstring = transformChar(c, row->par(), pos);
593 while (vpos <= last &&
594 (pos = vis2log(vpos)) >= 0
595 && IsPrintableNonspace(c = row->par()->getChar(pos))
596 && !Encodings::IsComposeChar_arabic(c)
597 && font2 == getFont(bview->buffer(), row->par(), pos)) {
598 c = transformChar(c, row->par(), pos);
602 // Draw text and set the new x position
603 pain.text(int(x), offset + row->baseline(),
605 x += lyxfont::width(textstring, font);
608 while (vpos <= last &&
609 (pos = vis2log(vpos)) >= 0
610 && IsPrintableNonspace(c = row->par()->getChar(pos))
611 && font2 == getFont(bview->buffer(), row->par(), pos)) {
615 // Draw text and set the new x position
616 pain.text(int(x), offset + row->baseline(), textstring, font);
617 x += lyxfont::width(textstring, font);
620 #ifdef INHERIT_LANGUAGE
622 if ((font.language() == inherit_language) ||
623 (font.language() == ignore_language))
624 lyxerr << "No this shouldn't happen!\n";
627 if (lyxrc.mark_foreign_language &&
628 font.language() != latex_language &&
629 font.language() != bview->buffer()->params.language) {
630 int const y = offset + row->height() - 1;
631 pain.line(int(tmpx), y, int(x), y,
635 // If we want ulem.sty support, drawing
636 // routines should go here. (Asger)
637 // Why shouldn't LyXFont::drawText handle it internally?
641 // Returns the left beginning of the text.
642 // This information cannot be taken from the layouts-objekt, because in
643 // LaTeX the beginning of the text fits in some cases (for example sections)
644 // exactly the label-width.
645 int LyXText::leftMargin(BufferView * bview, Row const * row) const
647 LyXTextClass const & tclass =
648 textclasslist.TextClass(bview->buffer()->params.textclass);
649 LyXLayout const & layout = tclass[row->par()->getLayout()];
651 string parindent = layout.parindent;
653 int x = LYX_PAPER_MARGIN;
655 x += lyxfont::signedWidth(tclass.leftmargin(), tclass.defaultfont());
657 // this is the way, LyX handles the LaTeX-Environments.
658 // I have had this idea very late, so it seems to be a
659 // later added hack and this is true
660 if (!row->par()->getDepth()) {
661 if (!row->par()->getLayout()) {
662 // find the previous same level paragraph
663 if (row->par()->previous()) {
664 Paragraph * newpar = row->par()
665 ->depthHook(row->par()->getDepth());
667 tclass[newpar->getLayout()].nextnoindent)
672 // find the next level paragraph
675 row->par()->outerHook();
677 // make a corresponding row. Needed to call LeftMargin()
679 // check wether it is a sufficent paragraph
680 if (newpar && tclass[newpar->getLayout()].isEnvironment())
683 dummyrow.par(newpar);
684 dummyrow.pos(newpar->size());
685 x = leftMargin(bview, &dummyrow);
687 // this is no longer an error, because this function
688 // is used to clear impossible depths after changing
689 // a layout. Since there is always a redo,
690 // LeftMargin() is always called
691 row->par()->params().depth(0);
694 if (newpar && !row->par()->getLayout()) {
695 if (newpar->params().noindent())
698 parindent = tclass[newpar->getLayout()].parindent;
703 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
704 switch (layout.margintype) {
706 if (!layout.leftmargin.empty()) {
707 x += lyxfont::signedWidth(layout.leftmargin,
708 tclass.defaultfont());
710 if (!row->par()->getLabelstring().empty()) {
711 x += lyxfont::signedWidth(layout.labelindent,
713 x += lyxfont::width(row->par()->getLabelstring(),
715 x += lyxfont::width(layout.labelsep, labelfont);
719 x += lyxfont::signedWidth(layout.labelindent, labelfont);
720 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
721 if (!row->par()->getLabelWidthString().empty()) {
722 x += lyxfont::width(row->par()->getLabelWidthString(),
724 x += lyxfont::width(layout.labelsep, labelfont);
729 x += lyxfont::signedWidth(layout.leftmargin, tclass.defaultfont()) * 4
730 / (row->par()->getDepth() + 4);
732 case MARGIN_FIRST_DYNAMIC:
733 if (layout.labeltype == LABEL_MANUAL) {
734 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
735 x += lyxfont::signedWidth(layout.leftmargin,
738 x += lyxfont::signedWidth(layout.labelindent,
741 } else if (row->pos()
742 // Special case to fix problems with
744 || (layout.labeltype == LABEL_STATIC
745 && layout.latextype == LATEX_ENVIRONMENT
746 && ! row->par()->isFirstInSequence())) {
747 x += lyxfont::signedWidth(layout.leftmargin,
749 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
750 && layout.labeltype != LABEL_BIBLIO
751 && layout.labeltype !=
752 LABEL_CENTERED_TOP_ENVIRONMENT) {
753 x += lyxfont::signedWidth(layout.labelindent,
755 x += lyxfont::width(layout.labelsep, labelfont);
756 x += lyxfont::width(row->par()->getLabelstring(),
761 case MARGIN_RIGHT_ADDRESS_BOX:
763 // ok, a terrible hack. The left margin depends on the widest
764 // row in this paragraph. Do not care about footnotes, they
765 // are *NOT* allowed in the LaTeX realisation of this layout.
767 // find the first row of this paragraph
768 Row const * tmprow = row;
769 while (tmprow->previous()
770 && tmprow->previous()->par() == row->par())
771 tmprow = tmprow->previous();
773 int minfill = tmprow->fill();
774 while (tmprow->next() && tmprow->next()->par() == row->par()) {
775 tmprow = tmprow->next();
776 if (tmprow->fill() < minfill)
777 minfill = tmprow->fill();
780 x += lyxfont::signedWidth(layout.leftmargin,
781 tclass.defaultfont());
787 LyXAlignment align; // wrong type
789 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
790 align = layout.align;
792 align = row->par()->params().align();
794 // set the correct parindent
795 if (row->pos() == 0) {
796 if ((layout.labeltype == LABEL_NO_LABEL
797 || layout.labeltype == LABEL_TOP_ENVIRONMENT
798 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
799 || (layout.labeltype == LABEL_STATIC
800 && layout.latextype == LATEX_ENVIRONMENT
801 && ! row->par()->isFirstInSequence()))
802 && align == LYX_ALIGN_BLOCK
803 && !row->par()->params().noindent()
804 && (row->par()->layout ||
805 bview->buffer()->params.paragraph_separation ==
806 BufferParams::PARSEP_INDENT))
807 x += lyxfont::signedWidth(parindent,
808 tclass.defaultfont());
809 else if (layout.labeltype == LABEL_BIBLIO) {
810 // ale970405 Right width for bibitems
811 x += bibitemMaxWidth(bview, tclass.defaultfont());
818 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
820 LyXTextClass const & tclass =
821 textclasslist.TextClass(buf->params.textclass);
822 LyXLayout const & layout = tclass[row->par()->getLayout()];
824 int x = LYX_PAPER_MARGIN
825 + lyxfont::signedWidth(tclass.rightmargin(),
826 tclass.defaultfont());
828 // this is the way, LyX handles the LaTeX-Environments.
829 // I have had this idea very late, so it seems to be a
830 // later added hack and this is true
831 if (row->par()->getDepth()) {
832 // find the next level paragraph
834 Paragraph * newpar = row->par();
837 newpar = newpar->previous();
839 && newpar->getDepth() >= row->par()->getDepth());
841 // make a corresponding row. Needed to call LeftMargin()
843 // check wether it is a sufficent paragraph
845 && tclass[newpar->getLayout()].isEnvironment()) {
847 dummyrow.par(newpar);
849 x = rightMargin(buf, &dummyrow);
851 // this is no longer an error, because this function
852 // is used to clear impossible depths after changing
853 // a layout. Since there is always a redo,
854 // LeftMargin() is always called
855 row->par()->params().depth(0);
859 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
860 x += lyxfont::signedWidth(layout.rightmargin, tclass.defaultfont())
861 * 4 / (row->par()->getDepth() + 4);
866 int LyXText::labelEnd(BufferView * bview, Row const * row) const
868 if (textclasslist.Style(bview->buffer()->params.textclass,
869 row->par()->getLayout()).margintype
873 tmprow.pos(row->par()->size());
874 return leftMargin(bview, &tmprow); /* just the beginning
877 return 0; /* LabelEnd is only needed, if the
878 layout fills a flushleft
883 // get the next breakpoint in a given paragraph
885 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
887 Paragraph * par = row->par();
892 Paragraph::size_type const pos = row->pos();
895 // position of the last possible breakpoint
896 // -1 isn't a suitable value, but a flag
897 Paragraph::size_type last_separator = -1;
898 width -= rightMargin(bview->buffer(), row);
900 Paragraph::size_type const main_body =
901 beginningOfMainBody(bview->buffer(), par);
902 LyXLayout const & layout =
903 textclasslist.Style(bview->buffer()->params.textclass,
905 Paragraph::size_type i = pos;
907 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
908 /* special code for right address boxes, only newlines count */
909 while (i < par->size()) {
910 if (par->isNewline(i)) {
912 i = par->size() - 1; // this means break
914 } else if (par->isInset(i) && par->getInset(i)
915 && par->getInset(i)->display()) {
916 par->getInset(i)->display(false);
921 // Last position is an invariant
922 Paragraph::size_type const last =
924 // this is the usual handling
925 int x = leftMargin(bview, row);
926 bool doitonetime = true;
927 while (doitonetime || ((x < width) && (i < last))) {
929 char const c = par->getChar(i);
930 if (IsNewlineChar(c)) {
932 x = width; // this means break
933 } else if (c == Paragraph::META_INSET &&
936 // check wether a Display() inset is
937 // valid here. if not, change it to
939 if (par->getInset(i)->display() &&
940 (layout.isCommand() ||
941 (layout.labeltype == LABEL_MANUAL
942 && i < beginningOfMainBody(bview->buffer(), par)))) {
943 // display istn't allowd
944 par->getInset(i)->display(false);
945 x += singleWidth(bview, par, i, c);
946 } else if (par->getInset(i)->display() ||
947 par->getInset(i)->needFullRow()) {
948 // So break the line here
952 if (IsLineSeparatorChar(par->getChar(i+1)))
955 last_separator = last; // to avoid extra rows
957 last_separator = i - 1;
958 x = width; // this means break
960 x += singleWidth(bview, par, i, c);
963 if (IsLineSeparatorChar(c))
965 x += singleWidth(bview, par, i, c);
968 if (i == main_body) {
969 x += lyxfont::width(layout.labelsep,
970 getLabelFont(bview->buffer(), par));
971 if (par->isLineSeparator(i - 1))
972 x-= singleWidth(bview, par, i - 1);
973 int left_margin = labelEnd(bview, row);
978 // end of paragraph is always a suitable separator
979 if (i == last && x < width)
983 // well, if last_separator is still 0, the line isn't breakable.
984 // don't care and cut simply at the end
985 if (last_separator < 0) {
989 // manual labels cannot be broken in LaTeX, do not care
990 if (main_body && last_separator < main_body)
991 last_separator = main_body - 1;
993 return last_separator;
997 // returns the minimum space a row needs on the screen in pixel
998 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1000 if (paper_width < 0)
1004 // get the pure distance
1005 Paragraph::size_type const last = rowLastPrintable(row);
1007 // special handling of the right address boxes
1008 if (textclasslist.Style(bview->buffer()->params.textclass,
1009 row->par()->getLayout()).margintype
1010 == MARGIN_RIGHT_ADDRESS_BOX)
1012 int const tmpfill = row->fill();
1013 row->fill(0); // the minfill in MarginLeft()
1014 w = leftMargin(bview, row);
1017 w = leftMargin(bview, row);
1019 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1020 row->par()->getLayout());
1021 Paragraph::size_type const main_body =
1022 beginningOfMainBody(bview->buffer(), row->par());
1023 Paragraph::size_type i = row->pos();
1026 if (main_body > 0 && i == main_body) {
1027 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1028 if (row->par()->isLineSeparator(i - 1))
1029 w -= singleWidth(bview, row->par(), i - 1);
1030 int left_margin = labelEnd(bview, row);
1031 if (w < left_margin)
1034 w += singleWidth(bview, row->par(), i);
1037 if (main_body > 0 && main_body > last) {
1038 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1039 if (last >= 0 && row->par()->isLineSeparator(last))
1040 w -= singleWidth(bview, row->par(), last);
1041 int const left_margin = labelEnd(bview, row);
1042 if (w < left_margin)
1046 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1047 #ifdef WITH_WARNINGS
1048 #warning Please fix me (Jug!)
1058 // returns the minimum space a manual label needs on the screen in pixel
1059 int LyXText::labelFill(BufferView * bview, Row const * row) const
1061 Paragraph::size_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1062 // -1 because a label ends either with a space that is in the label,
1063 // or with the beginning of a footnote that is outside the label.
1065 // I don't understand this code in depth, but sometimes "last" is
1066 // less than 0 and this causes a crash. This fix seems to work
1067 // correctly, but I bet the real error is elsewhere. The bug is
1068 // triggered when you have an open footnote in a paragraph
1069 // environment with a manual label. (Asger)
1070 if (last < 0) last = 0;
1072 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1077 Paragraph::size_type i = row->pos();
1079 w += singleWidth(bview, row->par(), i);
1084 if (!row->par()->params().labelWidthString().empty()) {
1085 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1086 getLabelFont(bview->buffer(), row->par())) - w,
1094 // returns the number of separators in the specified row. The separator
1095 // on the very last column doesnt count
1096 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1098 Paragraph::size_type const last = rowLast(row);
1099 Paragraph::size_type p =
1100 max(row->pos(), beginningOfMainBody(buf, row->par()));
1102 for (; p < last; ++p) {
1103 if (row->par()->isSeparator(p)) {
1111 // returns the number of hfills in the specified row. The LyX-Hfill is
1112 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1113 // ignored. This is *MUCH* more usefull than not to ignore!
1114 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1116 Paragraph::size_type const last = rowLast(row);
1117 Paragraph::size_type first = row->pos();
1118 if (first) { /* hfill *DO* count at the beginning
1120 while(first <= last && row->par()->isHfill(first))
1124 first = max(first, beginningOfMainBody(buf, row->par()));
1126 for (Paragraph::size_type p = first; p <= last; ++p) {
1127 // last, because the end is ignored!
1128 if (row->par()->isHfill(p)) {
1136 // like NumberOfHfills, but only those in the manual label!
1137 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1139 Paragraph::size_type last = rowLast(row);
1140 Paragraph::size_type first = row->pos();
1141 if (first) { /* hfill *DO* count at the beginning
1143 while(first < last && row->par()->isHfill(first))
1147 last = min(last, beginningOfMainBody(buf, row->par()));
1149 for (Paragraph::size_type p = first;
1150 p < last; ++p) { // last, because the end is ignored!
1151 if (row->par()->isHfill(p)) {
1159 // returns true, if a expansion is needed.
1160 // Rules are given by LaTeX
1161 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1162 Paragraph::size_type pos) const
1164 // by the way, is it a hfill?
1165 if (!row_ptr->par()->isHfill(pos))
1168 // at the end of a row it does not count
1169 if (pos >= rowLast(row_ptr))
1172 // at the beginning of a row it does not count, if it is not
1173 // the first row of a paragaph
1174 if (!row_ptr->pos())
1177 // in some labels it does not count
1178 if (textclasslist.Style(buf->params.textclass,
1179 row_ptr->par()->getLayout()).margintype
1181 && pos < beginningOfMainBody(buf, row_ptr->par()))
1184 // if there is anything between the first char of the row and
1185 // the sepcified position that is not a newline and not a hfill,
1186 // the hfill will count, otherwise not
1187 Paragraph::size_type i = row_ptr->pos();
1188 while (i < pos && (row_ptr->par()->isNewline(i)
1189 || row_ptr->par()->isHfill(i)))
1196 LColor::color LyXText::backgroundColor()
1199 return inset_owner->backgroundColor();
1201 return LColor::background;
1204 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1206 /* get the maximum ascent and the maximum descent */
1209 float layoutasc = 0;
1210 float layoutdesc = 0;
1213 Inset * tmpinset = 0;
1215 /* ok , let us initialize the maxasc and maxdesc value.
1216 * This depends in LaTeX of the font of the last character
1217 * in the paragraph. The hack below is necessary because
1218 * of the possibility of open footnotes */
1220 /* Correction: only the fontsize count. The other properties
1221 are taken from the layoutfont. Nicer on the screen :) */
1222 Paragraph * par = row_ptr->par();
1223 Paragraph * firstpar = row_ptr->par();
1225 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1226 firstpar->getLayout());
1228 // as max get the first character of this row then it can increes but not
1229 // decrees the height. Just some point to start with so we don't have to
1230 // do the assignment below too often.
1231 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1232 LyXFont::FONT_SIZE const tmpsize = font.size();
1233 font = getLayoutFont(bview->buffer(), par);
1234 LyXFont::FONT_SIZE const size = font.size();
1235 font.setSize(tmpsize);
1237 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1239 float spacing_val = 1.0;
1240 if (!row_ptr->par()->params().spacing().isDefault()) {
1241 spacing_val = row_ptr->par()->params().spacing().getValue();
1243 spacing_val = bview->buffer()->params.spacing.getValue();
1245 //lyxerr << "spacing_val = " << spacing_val << endl;
1247 int maxasc = int(lyxfont::maxAscent(font) *
1248 layout.spacing.getValue() *
1250 int maxdesc = int(lyxfont::maxDescent(font) *
1251 layout.spacing.getValue() *
1253 Paragraph::size_type const pos_end = rowLast(row_ptr);
1257 // Check if any insets are larger
1258 for (Paragraph::size_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1259 if (row_ptr->par()->isInset(pos)) {
1260 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1261 tmpinset = row_ptr->par()->getInset(pos);
1263 #if 1 // this is needed for deep update on initialitation
1264 tmpinset->update(bview, tmpfont);
1266 asc = tmpinset->ascent(bview, tmpfont);
1267 desc = tmpinset->descent(bview, tmpfont);
1268 maxwidth += tmpinset->width(bview, tmpfont);
1269 maxasc = max(maxasc, asc);
1270 maxdesc = max(maxdesc, desc);
1273 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1277 // Check if any custom fonts are larger (Asger)
1278 // This is not completely correct, but we can live with the small,
1279 // cosmetic error for now.
1280 LyXFont::FONT_SIZE maxsize =
1281 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1282 if (maxsize > font.size()) {
1283 font.setSize(maxsize);
1285 asc = lyxfont::maxAscent(font);
1286 desc = lyxfont::maxDescent(font);
1293 // This is nicer with box insets:
1297 row_ptr->ascent_of_text(maxasc);
1299 // is it a top line?
1300 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1302 // some parksips VERY EASY IMPLEMENTATION
1303 if (bview->buffer()->params.paragraph_separation ==
1304 BufferParams::PARSEP_SKIP)
1306 if (layout.isParagraph()
1307 && firstpar->getDepth() == 0
1308 && firstpar->previous())
1310 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1311 } else if (firstpar->previous() &&
1312 textclasslist.Style(bview->buffer()->params.textclass,
1313 firstpar->previous()->
1314 getLayout()).isParagraph() &&
1315 firstpar->previous()->getDepth() == 0)
1317 // is it right to use defskip here too? (AS)
1318 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1322 // the paper margins
1323 if (!row_ptr->par()->previous() && bv_owner)
1324 maxasc += LYX_PAPER_MARGIN;
1326 // add the vertical spaces, that the user added
1327 if (firstpar->params().spaceTop().kind() != VSpace::NONE)
1328 maxasc += int(firstpar->params().spaceTop().inPixels(bview));
1330 // do not forget the DTP-lines!
1331 // there height depends on the font of the nearest character
1332 if (firstpar->params().lineTop())
1333 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1336 // and now the pagebreaks
1337 if (firstpar->params().pagebreakTop())
1338 maxasc += 3 * defaultHeight();
1340 // This is special code for the chapter, since the label of this
1341 // layout is printed in an extra row
1342 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1343 && bview->buffer()->params.secnumdepth >= 0)
1345 float spacing_val = 1.0;
1346 if (!row_ptr->par()->params().spacing().isDefault()) {
1347 spacing_val = row_ptr->par()->params().spacing().getValue();
1349 spacing_val = bview->buffer()->params.spacing.getValue();
1352 labeladdon = int(lyxfont::maxDescent(labelfont) *
1353 layout.spacing.getValue() *
1355 + int(lyxfont::maxAscent(labelfont) *
1356 layout.spacing.getValue() *
1360 // special code for the top label
1361 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1362 || layout.labeltype == LABEL_BIBLIO
1363 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1364 && row_ptr->par()->isFirstInSequence()
1365 && !row_ptr->par()->getLabelstring().empty())
1367 float spacing_val = 1.0;
1368 if (!row_ptr->par()->params().spacing().isDefault()) {
1369 spacing_val = row_ptr->par()->params().spacing().getValue();
1371 spacing_val = bview->buffer()->params.spacing.getValue();
1375 (lyxfont::maxAscent(labelfont) *
1376 layout.spacing.getValue() *
1378 +(lyxfont::maxDescent(labelfont) *
1379 layout.spacing.getValue() *
1381 + layout.topsep * defaultHeight()
1382 + layout.labelbottomsep * defaultHeight());
1385 // and now the layout spaces, for example before and after a section,
1386 // or between the items of a itemize or enumerate environment
1388 if (!firstpar->params().pagebreakTop()) {
1389 Paragraph * prev = row_ptr->par()->previous();
1391 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1392 if (prev && prev->getLayout() == firstpar->getLayout() &&
1393 prev->getDepth() == firstpar->getDepth() &&
1394 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1396 layoutasc = (layout.itemsep * defaultHeight());
1397 } else if (row_ptr->previous()) {
1398 tmptop = layout.topsep;
1400 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1401 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1402 row_ptr->previous()->par()->
1403 getLayout()).bottomsep;
1406 layoutasc = (tmptop * defaultHeight());
1407 } else if (row_ptr->par()->params().lineTop()) {
1408 tmptop = layout.topsep;
1411 layoutasc = (tmptop * defaultHeight());
1414 prev = row_ptr->par()->outerHook();
1416 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1417 prev->getLayout()).parsep * defaultHeight());
1419 if (firstpar->previous() &&
1420 firstpar->previous()->getDepth() == 0 &&
1421 firstpar->previous()->getLayout() !=
1422 firstpar->getLayout())
1425 } else if (firstpar->previous()) {
1426 maxasc += int(layout.parsep * defaultHeight());
1432 // is it a bottom line?
1433 if (row_ptr->par() == par
1434 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1436 // the paper margins
1437 if (!par->next() && bv_owner)
1438 maxdesc += LYX_PAPER_MARGIN;
1440 // add the vertical spaces, that the user added
1441 if (firstpar->params().spaceBottom().kind() != VSpace::NONE)
1442 maxdesc += int(firstpar->params().spaceBottom().inPixels(bview));
1444 // do not forget the DTP-lines!
1445 // there height depends on the font of the nearest character
1446 if (firstpar->params().lineBottom())
1447 maxdesc += 2 * lyxfont::ascent('x',
1448 getFont(bview->buffer(),
1450 max(Paragraph::size_type(0), par->size() - 1)));
1452 // and now the pagebreaks
1453 if (firstpar->params().pagebreakBottom())
1454 maxdesc += 3 * defaultHeight();
1456 // and now the layout spaces, for example before and after
1457 // a section, or between the items of a itemize or enumerate
1459 if (!firstpar->params().pagebreakBottom() && row_ptr->par()->next()) {
1460 Paragraph * nextpar = row_ptr->par()->next();
1461 Paragraph * comparepar = row_ptr->par();
1465 if (comparepar->getDepth() > nextpar->getDepth()) {
1466 usual = (textclasslist.Style(bview->buffer()->params.textclass,
1467 comparepar->getLayout()).bottomsep * defaultHeight());
1468 comparepar = comparepar->depthHook(nextpar->getDepth());
1469 if (comparepar->getLayout()!= nextpar->getLayout()
1470 || nextpar->getLabelWidthString() !=
1471 comparepar->getLabelWidthString())
1473 unusual = (textclasslist.Style(bview->buffer()->params.textclass,
1474 comparepar->getLayout()).bottomsep * defaultHeight());
1476 if (unusual > usual)
1477 layoutdesc = unusual;
1480 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1482 if (comparepar->getLayout()!= nextpar->getLayout()
1483 || nextpar->getLabelWidthString() !=
1484 comparepar->getLabelWidthString())
1485 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
1486 comparepar->getLayout()).bottomsep * defaultHeight());
1491 // incalculate the layout spaces
1492 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1493 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1495 // calculate the new height of the text
1496 height -= row_ptr->height();
1498 row_ptr->height(maxasc + maxdesc + labeladdon);
1499 row_ptr->baseline(maxasc + labeladdon);
1501 height += row_ptr->height();
1503 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1505 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1507 row_ptr->width(int(maxwidth + x));
1510 width = max(0,workWidth(bview));
1512 if (r->width() > width)
1520 /* Appends the implicit specified paragraph behind the specified row,
1521 * start at the implicit given position */
1522 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1524 bool not_ready = true;
1526 // The last character position of a paragraph is an invariant so we can
1527 // safely get it here. (Asger)
1528 Paragraph::size_type const lastposition = row->par()->size();
1530 // Get the next breakpoint
1531 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1535 // Insert the new row
1536 if (z < lastposition) {
1538 insertRow(row, row->par(), z);
1545 // Set the dimensions of the row
1546 #ifdef WITH_WARNINGS
1547 #warning Something is rotten here! (Jug)
1549 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1550 setHeightOfRow(bview, tmprow);
1552 } while (not_ready);
1556 void LyXText::breakAgain(BufferView * bview, Row * row) const
1558 bool not_ready = true;
1561 // get the next breakpoint
1562 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1565 if (z < row->par()->size()) {
1566 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1569 insertRow(row, row->par(), z);
1575 if (row->pos() == z)
1576 not_ready = false; // the rest will not change
1582 /* if there are some rows too much, delete them */
1583 /* only if you broke the whole paragraph! */
1584 Row * tmprow2 = row;
1585 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1586 tmprow2 = tmprow2->next();
1588 while (tmprow2 != row) {
1589 tmprow2 = tmprow2->previous();
1590 removeRow(tmprow2->next());
1595 /* set the dimensions of the row */
1596 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1597 setHeightOfRow(bview, tmprow);
1598 } while (not_ready);
1602 // this is just a little changed version of break again
1603 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1605 // get the next breakpoint
1606 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1609 if (z < row->par()->size()) {
1611 || (row->next() && row->next()->par() != row->par())) {
1612 /* insert a new row */
1614 insertRow(row, row->par(), z);
1620 if (row->pos() != z)
1624 // if there are some rows too much, delete them
1625 // only if you broke the whole paragraph!
1626 Row * tmprow2 = row;
1627 while (tmprow2->next()
1628 && tmprow2->next()->par() == row->par()) {
1629 tmprow2 = tmprow2->next();
1631 while (tmprow2 != row) {
1632 tmprow2 = tmprow2->previous();
1633 removeRow(tmprow2->next());
1637 // set the dimensions of the row
1638 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1639 setHeightOfRow(bview, tmprow);
1643 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1645 LyXLayout const & layout =
1646 textclasslist.Style(bview->buffer()->params.textclass,
1647 cursor.par()->getLayout());
1649 // this is only allowed, if the current paragraph is not empty or caption
1650 if ((cursor.par()->size() <= 0)
1651 && layout.labeltype!= LABEL_SENSITIVE)
1654 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1656 // Always break behind a space
1658 // It is better to erase the space (Dekel)
1659 if (cursor.pos() < cursor.par()->size()
1660 && cursor.par()->isLineSeparator(cursor.pos()))
1661 cursor.par()->erase(cursor.pos());
1662 // cursor.pos(cursor.pos() + 1);
1664 // break the paragraph
1668 keep_layout = layout.isEnvironment();
1669 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1672 // well this is the caption hack since one caption is really enough
1673 if (layout.labeltype == LABEL_SENSITIVE) {
1675 // set to standard-layout
1676 cursor.par()->setLayout(0);
1678 // set to standard-layout
1679 cursor.par()->next()->setLayout(0);
1682 /* if the cursor is at the beginning of a row without prior newline,
1684 * This touches only the screen-update. Otherwise we would may have
1685 * an empty row on the screen */
1686 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1687 && cursor.row()->pos() == cursor.pos()) {
1691 status(bview, LyXText::NEED_MORE_REFRESH);
1692 refresh_row = cursor.row();
1693 refresh_y = cursor.y() - cursor.row()->baseline();
1695 // Do not forget the special right address boxes
1696 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1697 while (refresh_row->previous() &&
1698 refresh_row->previous()->par() == refresh_row->par()) {
1699 refresh_row = refresh_row->previous();
1700 refresh_y -= refresh_row->height();
1703 removeParagraph(cursor.row());
1705 // set the dimensions of the cursor row
1706 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1708 setHeightOfRow(bview, cursor.row());
1710 while (cursor.par()->next()->size()
1711 && cursor.par()->next()->isNewline(0))
1712 cursor.par()->next()->erase(0);
1714 insertParagraph(bview, cursor.par()->next(), cursor.row());
1716 updateCounters(bview, cursor.row()->previous());
1718 /* This check is necessary. Otherwise the new empty paragraph will
1719 * be deleted automatically. And it is more friendly for the user! */
1721 setCursor(bview, cursor.par()->next(), 0);
1723 setCursor(bview, cursor.par(), 0);
1725 if (cursor.row()->next())
1726 breakAgain(bview, cursor.row()->next());
1732 // Just a macro to make some thing easier.
1733 void LyXText::redoParagraph(BufferView * bview) const
1736 redoParagraphs(bview, cursor, cursor.par()->next());
1737 setCursorIntern(bview, cursor.par(), cursor.pos());
1741 /* insert a character, moves all the following breaks in the
1742 * same Paragraph one to the right and make a rebreak */
1743 void LyXText::insertChar(BufferView * bview, char c)
1745 setUndo(bview, Undo::INSERT,
1746 cursor.par(), cursor.par()->next());
1748 // When the free-spacing option is set for the current layout,
1749 // disable the double-space checking
1751 bool const freeSpacing =
1752 textclasslist.Style(bview->buffer()->params.textclass,
1753 cursor.row()->par()->getLayout()).free_spacing;
1756 if (lyxrc.auto_number) {
1757 static string const number_operators = "+-/*";
1758 static string const number_unary_operators = "+-";
1759 static string const number_seperators = ".,:";
1761 if (current_font.number() == LyXFont::ON) {
1762 if (!isdigit(c) && !contains(number_operators, c) &&
1763 !(contains(number_seperators, c) &&
1764 cursor.pos() >= 1 &&
1765 cursor.pos() < cursor.par()->size() &&
1766 getFont(bview->buffer(),
1768 cursor.pos()).number() == LyXFont::ON &&
1769 getFont(bview->buffer(),
1771 cursor.pos() - 1).number() == LyXFont::ON)
1773 number(bview); // Set current_font.number to OFF
1774 } else if (isdigit(c) &&
1775 real_current_font.isVisibleRightToLeft()) {
1776 number(bview); // Set current_font.number to ON
1778 if (cursor.pos() > 0) {
1779 char const c = cursor.par()->getChar(cursor.pos() - 1);
1780 if (contains(number_unary_operators, c) &&
1781 (cursor.pos() == 1 ||
1782 cursor.par()->isSeparator(cursor.pos() - 2) ||
1783 cursor.par()->isNewline(cursor.pos() - 2) )
1785 setCharFont(bview->buffer(),
1789 } else if (contains(number_seperators, c) &&
1790 cursor.pos() >= 2 &&
1791 getFont(bview->buffer(),
1793 cursor.pos() - 2).number() == LyXFont::ON) {
1794 setCharFont(bview->buffer(),
1804 /* First check, if there will be two blanks together or a blank at
1805 the beginning of a paragraph.
1806 I decided to handle blanks like normal characters, the main
1807 difference are the special checks when calculating the row.fill
1808 (blank does not count at the end of a row) and the check here */
1810 // The bug is triggered when we type in a description environment:
1811 // The current_font is not changed when we go from label to main text
1812 // and it should (along with realtmpfont) when we type the space.
1813 // CHECK There is a bug here! (Asger)
1815 LyXFont realtmpfont = real_current_font;
1816 LyXFont rawtmpfont = current_font; /* store the current font.
1817 * This is because of the use
1818 * of cursor movements. The moving
1819 * cursor would refresh the
1822 // Get the font that is used to calculate the baselineskip
1823 Paragraph::size_type const lastpos = cursor.par()->size();
1824 LyXFont rawparfont =
1825 cursor.par()->getFontSettings(bview->buffer()->params,
1828 bool jumped_over_space = false;
1830 if (!freeSpacing && IsLineSeparatorChar(c)) {
1831 if ((cursor.pos() > 0
1832 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1833 || (cursor.pos() > 0
1834 && cursor.par()->isNewline(cursor.pos() - 1))
1835 || (cursor.pos() == 0)) {
1836 static bool sent_space_message = false;
1837 if (!sent_space_message) {
1838 if (cursor.pos() == 0)
1839 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1841 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1842 sent_space_message = true;
1847 } else if (IsNewlineChar(c)) {
1848 if (cursor.par() == cursor.par()
1849 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1853 /* No newline at first position
1854 * of a paragraph or behind labels.
1855 * TeX does not allow that. */
1857 if (cursor.pos() < cursor.par()->size() &&
1858 cursor.par()->isLineSeparator(cursor.pos()))
1859 // newline always after a blank!
1861 cursor.row()->fill(-1); // to force a new break
1864 // the display inset stuff
1865 if (cursor.row()->par()->isInset(cursor.row()->pos())
1866 && cursor.row()->par()->getInset(cursor.row()->pos())
1867 && (cursor.row()->par()->getInset(cursor.row()->pos())->display() ||
1868 cursor.row()->par()->getInset(cursor.row()->pos())->needFullRow()))
1869 cursor.row()->fill(-1); // to force a new break
1871 // get the cursor row fist
1872 Row * row = cursor.row();
1873 int y = cursor.y() - row->baseline();
1874 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1875 * already insertet the character */
1876 cursor.par()->insertChar(cursor.pos(), c);
1877 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1879 if (!jumped_over_space) {
1880 // refresh the positions
1882 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1883 tmprow = tmprow->next();
1884 tmprow->pos(tmprow->pos() + 1);
1888 // Is there a break one row above
1889 if ((cursor.par()->isLineSeparator(cursor.pos())
1890 || cursor.par()->isNewline(cursor.pos())
1891 || cursor.row()->fill() == -1)
1892 && row->previous() && row->previous()->par() == row->par()) {
1893 Paragraph::size_type z = nextBreakPoint(bview,
1896 if (z >= row->pos()) {
1899 // set the dimensions of the row above
1900 row->previous()->fill(fill(bview,
1904 setHeightOfRow(bview, row->previous());
1906 y -= row->previous()->height();
1908 refresh_row = row->previous();
1909 status(bview, LyXText::NEED_MORE_REFRESH);
1911 breakAgainOneRow(bview, row);
1913 current_font = rawtmpfont;
1914 real_current_font = realtmpfont;
1915 setCursor(bview, cursor.par(), cursor.pos() + 1,
1916 false, cursor.boundary());
1917 // cursor MUST be in row now.
1919 if (row->next() && row->next()->par() == row->par())
1920 need_break_row = row->next();
1924 // check, wether the last characters font has changed.
1925 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1926 && rawparfont != rawtmpfont)
1927 redoHeightOfParagraph(bview, cursor);
1934 // recalculate the fill of the row
1935 if (row->fill() >= 0) /* needed because a newline
1936 * will set fill to -1. Otherwise
1937 * we would not get a rebreak! */
1938 row->fill(fill(bview, row, workWidth(bview)));
1939 if (row->fill() < 0) {
1942 refresh_x = cursor.x();
1943 refresh_pos = cursor.pos();
1944 status(bview, LyXText::NEED_MORE_REFRESH);
1945 breakAgainOneRow(bview, row);
1946 // will the cursor be in another row now?
1947 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1948 if (row->next() && row->next()->par() == row->par())
1949 // this should always be true
1951 breakAgainOneRow(bview, row);
1953 current_font = rawtmpfont;
1954 real_current_font = realtmpfont;
1956 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1958 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1959 != cursor.boundary())
1960 setCursor(bview, cursor.par(), cursor.pos(), false,
1961 !cursor.boundary());
1962 if (row->next() && row->next()->par() == row->par())
1963 need_break_row = row->next();
1968 refresh_x = cursor.x();
1970 refresh_pos = cursor.pos();
1972 int const tmpheight = row->height();
1973 setHeightOfRow(bview, row);
1974 if (tmpheight == row->height())
1975 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
1977 status(bview, LyXText::NEED_MORE_REFRESH);
1979 current_font = rawtmpfont;
1980 real_current_font = realtmpfont;
1981 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1985 // check, wether the last characters font has changed.
1986 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1987 && rawparfont != rawtmpfont) {
1988 redoHeightOfParagraph(bview, cursor);
1990 // now the special right address boxes
1991 if (textclasslist.Style(bview->buffer()->params.textclass,
1992 cursor.par()->getLayout()).margintype
1993 == MARGIN_RIGHT_ADDRESS_BOX) {
1994 redoDrawingOfParagraph(bview, cursor);
2002 void LyXText::charInserted()
2004 // Here we could call FinishUndo for every 20 characters inserted.
2005 // This is from my experience how emacs does it.
2006 static unsigned int counter;
2016 void LyXText::prepareToPrint(BufferView * bview,
2017 Row * row, float & x,
2018 float & fill_separator,
2020 float & fill_label_hfill,
2026 float w = row->fill();
2028 fill_label_hfill = 0;
2030 fill_label_hfill = 0;
2033 row->par()->isRightToLeftPar(bview->buffer()->params);
2035 x = (workWidth(bview) > 0)
2036 ? rightMargin(bview->buffer(), row) : 0;
2038 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2040 // is there a manual margin with a manual label
2041 if (textclasslist.Style(bview->buffer()->params.textclass,
2042 row->par()->getLayout()).margintype == MARGIN_MANUAL
2043 && textclasslist.Style(bview->buffer()->params.textclass,
2044 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
2046 /* one more since labels are left aligned */
2047 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2048 if (nlh && !row->par()->getLabelWidthString().empty()) {
2049 fill_label_hfill = labelFill(bview, row) / nlh;
2053 // are there any hfills in the row?
2054 float const nh = numberOfHfills(bview->buffer(), row);
2058 fill_hfill = w / nh;
2060 // is it block, flushleft or flushright?
2061 // set x how you need it
2063 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2064 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2066 align = row->par()->params().align();
2069 // center displayed insets
2071 if (row->par()->isInset(row->pos())
2072 && (inset=row->par()->getInset(row->pos()))
2073 && (inset->display())) // || (inset->scroll() < 0)))
2074 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2075 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2078 case LYX_ALIGN_BLOCK:
2079 ns = numberOfSeparators(bview->buffer(), row);
2080 if (ns && row->next() && row->next()->par() == row->par() &&
2081 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2082 && !(row->next()->par()->isInset(row->next()->pos())
2083 && row->next()->par()->getInset(row->next()->pos())
2084 && row->next()->par()->getInset(row->next()->pos())->display())
2087 fill_separator = w / ns;
2088 } else if (is_rtl) {
2092 case LYX_ALIGN_RIGHT:
2095 case LYX_ALIGN_CENTER:
2103 computeBidiTables(bview->buffer(), row);
2105 Paragraph::size_type main_body =
2106 beginningOfMainBody(bview->buffer(), row->par());
2107 Paragraph::size_type last = rowLast(row);
2109 if (main_body > 0 &&
2110 (main_body-1 > last ||
2111 !row->par()->isLineSeparator(main_body-1))) {
2112 LyXLayout const & layout =
2113 textclasslist.Style(bview->buffer()->params.textclass,
2114 row->par()->getLayout());
2115 x += lyxfont::width(layout.labelsep,
2116 getLabelFont(bview->buffer(), row->par()));
2117 if (main_body-1 <= last)
2118 x += fill_label_hfill;
2123 /* important for the screen */
2126 /* the cursor set functions have a special mechanism. When they
2127 * realize, that you left an empty paragraph, they will delete it.
2128 * They also delete the corresponding row */
2130 void LyXText::cursorRightOneWord(BufferView * bview) const
2132 // treat floats, HFills and Insets as words
2133 LyXCursor tmpcursor = cursor;
2134 // CHECK See comment on top of text.C
2136 if (tmpcursor.pos() == tmpcursor.par()->size()
2137 && tmpcursor.par()->next()) {
2138 tmpcursor.par(tmpcursor.par()->next());
2143 // Skip through initial nonword stuff.
2144 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2145 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2146 // printf("Current pos1 %d", tmpcursor.pos()) ;
2147 tmpcursor.pos(tmpcursor.pos() + 1);
2150 // Advance through word.
2151 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2152 tmpcursor.par()->isWord( tmpcursor.pos())) {
2153 // printf("Current pos2 %d", tmpcursor.pos()) ;
2154 tmpcursor.pos(tmpcursor.pos() + 1);
2158 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2162 void LyXText::cursorTab(BufferView * bview) const
2164 LyXCursor tmpcursor = cursor;
2165 while (tmpcursor.pos() < tmpcursor.par()->size()
2166 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2167 tmpcursor.pos(tmpcursor.pos() + 1);
2169 if (tmpcursor.pos() == tmpcursor.par()->size()){
2170 if (tmpcursor.par()->next()) {
2171 tmpcursor.par(tmpcursor.par()->next());
2175 tmpcursor.pos(tmpcursor.pos() + 1);
2176 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2180 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2181 of prior word, not to end of next prior word. */
2183 void LyXText::cursorLeftOneWord(BufferView * bview) const
2185 LyXCursor tmpcursor = cursor;
2186 cursorLeftOneWord(tmpcursor);
2187 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2190 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2192 // treat HFills, floats and Insets as words
2195 && (cur.par()->isSeparator(cur.pos() - 1)
2196 || cur.par()->isKomma(cur.pos() - 1))
2197 && !(cur.par()->isHfill(cur.pos() - 1)
2198 || cur.par()->isInset(cur.pos() - 1)))
2199 cur.pos(cur.pos() - 1);
2202 && (cur.par()->isInset(cur.pos() - 1)
2203 || cur.par()->isHfill(cur.pos() - 1))) {
2204 cur.pos(cur.pos() - 1);
2205 } else if (!cur.pos()) {
2206 if (cur.par()->previous()){
2207 cur.par(cur.par()->previous());
2208 cur.pos(cur.par()->size());
2210 } else { // Here, cur != 0
2211 while (cur.pos() > 0 &&
2212 cur.par()->isWord(cur.pos()-1) )
2213 cur.pos(cur.pos() - 1);
2217 /* -------> Select current word. This depends on behaviour of
2218 CursorLeftOneWord(), so it is patched as well. */
2219 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2220 word_location const loc) const
2222 // first put the cursor where we wana start to select the word
2225 case WHOLE_WORD_STRICT:
2226 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2227 || cursor.par()->isSeparator(cursor.pos())
2228 || cursor.par()->isKomma(cursor.pos())
2229 || cursor.par()->isSeparator(cursor.pos() -1)
2230 || cursor.par()->isKomma(cursor.pos() -1)) {
2234 // no break here, we go to the next
2237 // Move cursor to the beginning, when not already there.
2238 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2239 && !from.par()->isKomma(from.pos() - 1))
2240 cursorLeftOneWord(from);
2243 // always move the cursor to the beginning of previous word
2244 cursorLeftOneWord(from);
2247 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2253 while (to.pos() < to.par()->size()
2254 && !to.par()->isSeparator(to.pos())
2255 && !to.par()->isKomma(to.pos())
2256 && !to.par()->isHfill(to.pos()) )
2258 to.pos(to.pos() + 1);
2263 void LyXText::selectWord(BufferView * bview, word_location const loc)
2267 getWord(from, to, loc);
2269 setCursor(bview, from.par(), from.pos());
2272 selection.cursor = cursor;
2273 setCursor(bview, to.par(), to.pos() );
2274 setSelection(bview);
2278 /* -------> Select the word currently under the cursor when no
2279 selection is currently set */
2280 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2281 word_location const loc)
2283 if (!selection.set()) {
2284 selectWord(bview, loc);
2285 return selection.set();
2291 // This function is only used by the spellchecker for NextWord().
2292 // It doesn't handle LYX_ACCENTs and probably never will.
2293 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2294 float & value) const
2296 if (the_locking_inset) {
2297 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2299 value += float(cursor.y())/float(height);
2302 #warning Dekel please have a look on this one RTL? (Jug)
2304 // we have to go on checking so move cusor to the right
2305 if (cursor.pos() == cursor.par()->size()) {
2306 if (!cursor.par()->next())
2308 cursor.par(cursor.par()->next());
2311 cursor.pos(cursor.pos() + 1);
2313 Paragraph * tmppar = cursor.par();
2315 // If this is not the very first word, skip rest of
2316 // current word because we are probably in the middle
2317 // of a word if there is text here.
2318 if (cursor.pos() || cursor.par()->previous()) {
2319 while (cursor.pos() < cursor.par()->size()
2320 && cursor.par()->isLetter(cursor.pos()))
2321 cursor.pos(cursor.pos() + 1);
2324 // Now, skip until we have real text (will jump paragraphs)
2325 while ((cursor.par()->size() > cursor.pos()
2326 && (!cursor.par()->isLetter(cursor.pos()))
2327 && (!cursor.par()->isInset(cursor.pos()) ||
2328 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2329 || (cursor.par()->size() == cursor.pos()
2330 && cursor.par()->next()))
2332 if (cursor.pos() == cursor.par()->size()) {
2333 cursor.par(cursor.par()->next());
2336 cursor.pos(cursor.pos() + 1);
2339 // now check if we hit an inset so it has to be a inset containing text!
2340 if (cursor.pos() < cursor.par()->size() &&
2341 cursor.par()->isInset(cursor.pos()))
2344 cursor.par()->getInset(cursor.pos())->edit(bview);
2345 // now call us again to do the above trick
2346 // but obviously we have to start from down below ;)
2347 return bview->text->selectNextWordToSpellcheck(bview, value);
2350 // Update the value if we changed paragraphs
2351 if (cursor.par() != tmppar){
2352 setCursor(bview, cursor.par(), cursor.pos());
2353 value = float(cursor.y())/float(height);
2356 // Start the selection from here
2357 selection.cursor = cursor;
2359 // and find the end of the word (insets like optional hyphens
2360 // and ligature break are part of a word)
2361 while (cursor.pos() < cursor.par()->size()
2362 && (cursor.par()->isLetter(cursor.pos())))
2363 cursor.pos(cursor.pos() + 1);
2365 // Finally, we copy the word to a string and return it
2367 if (selection.cursor.pos() < cursor.pos()) {
2368 Paragraph::size_type i;
2369 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2370 if (!cursor.par()->isInset(i))
2371 str += cursor.par()->getChar(i);
2378 // This one is also only for the spellchecker
2379 void LyXText::selectSelectedWord(BufferView * bview)
2381 if (the_locking_inset) {
2382 the_locking_inset->selectSelectedWord(bview);
2385 // move cursor to the beginning
2386 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2388 // set the sel cursor
2389 selection.cursor = cursor;
2391 // now find the end of the word
2392 while (cursor.pos() < cursor.par()->size()
2393 && (cursor.par()->isLetter(cursor.pos())))
2394 cursor.pos(cursor.pos() + 1);
2396 setCursor(bview, cursor.par(), cursor.pos());
2398 // finally set the selection
2399 setSelection(bview);
2403 /* -------> Delete from cursor up to the end of the current or next word. */
2404 void LyXText::deleteWordForward(BufferView * bview)
2406 if (!cursor.par()->size())
2409 LyXCursor tmpcursor = cursor;
2410 tmpcursor.row(0); //Â ??
2411 selection.set(true); // to avoid deletion
2412 cursorRightOneWord(bview);
2413 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2414 selection.cursor = cursor;
2416 setSelection(bview);
2418 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2419 cutSelection(bview, true, false);
2424 /* -------> Delete from cursor to start of current or prior word. */
2425 void LyXText::deleteWordBackward(BufferView * bview)
2427 if (!cursor.par()->size())
2430 LyXCursor tmpcursor = cursor;
2431 tmpcursor.row(0); // ??
2432 selection.set(true); // to avoid deletion
2433 cursorLeftOneWord(bview);
2434 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2435 selection.cursor = cursor;
2437 setSelection(bview);
2438 cutSelection(bview, true, false);
2443 /* -------> Kill to end of line. */
2444 void LyXText::deleteLineForward(BufferView * bview)
2446 if (!cursor.par()->size())
2447 // Paragraph is empty, so we just go to the right
2450 LyXCursor tmpcursor = cursor;
2451 // We can't store the row over a regular setCursor
2452 // so we set it to 0 and reset it afterwards.
2453 tmpcursor.row(0); //Â ??
2454 selection.set(true); // to avoid deletion
2456 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2457 selection.cursor = cursor;
2459 setSelection(bview);
2460 // What is this test for ??? (JMarc)
2461 if (!selection.set()) {
2462 deleteWordForward(bview);
2464 cutSelection(bview, true, false);
2470 // Change the case of a word at cursor position.
2471 // This function directly manipulates Paragraph::text because there
2472 // is no Paragraph::SetChar currently. I did what I could to ensure
2473 // that it is correct. I guess part of it should be moved to
2474 // Paragraph, but it will have to change for 1.1 anyway. At least
2475 // it does not access outside of the allocated array as the older
2476 // version did. (JMarc)
2477 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2482 if (selection.set()) {
2483 from = selection.start;
2486 getWord(from, to, PARTIAL_WORD);
2487 setCursor(bview, to.par(), to.pos() + 1);
2490 changeRegionCase(bview, from, to, action);
2494 void LyXText::changeRegionCase(BufferView * bview,
2495 LyXCursor const & from,
2496 LyXCursor const & to,
2497 LyXText::TextCase action)
2499 lyx::Assert(from <= to);
2501 setUndo(bview, Undo::FINISH,
2502 from.par(), to.par()->next());
2504 Paragraph::size_type pos = from.pos();
2505 Paragraph * par = from.par();
2507 while (par && (pos != to.pos() || par != to.par())) {
2508 unsigned char c = par->getChar(pos);
2509 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2511 case text_lowercase:
2514 case text_capitalization:
2516 action = text_lowercase;
2518 case text_uppercase:
2523 par->setChar(pos, c);
2524 checkParagraph(bview, par, pos);
2527 if (pos == par->size()) {
2532 if (to.row() != from.row()) {
2533 refresh_y = from.y() - from.row()->baseline();
2534 refresh_row = from.row();
2535 status(bview, LyXText::NEED_MORE_REFRESH);
2540 void LyXText::transposeChars(BufferView & bview)
2542 Paragraph * tmppar = cursor.par();
2544 setUndo(&bview, Undo::FINISH,
2545 tmppar, tmppar->next());
2547 Paragraph::size_type tmppos = cursor.pos();
2549 // First decide if it is possible to transpose at all
2551 // We are at the beginning of a paragraph.
2552 if (tmppos == 0) return;
2554 // We are at the end of a paragraph.
2555 if (tmppos == tmppar->size() - 1) return;
2557 unsigned char c1 = tmppar->getChar(tmppos);
2558 unsigned char c2 = tmppar->getChar(tmppos - 1);
2560 if (c1 != Paragraph::META_INSET
2561 && c2 != Paragraph::META_INSET) {
2562 tmppar->setChar(tmppos, c2);
2563 tmppar->setChar(tmppos - 1, c1);
2565 // We should have an implementation that handles insets
2566 // as well, but that will have to come later. (Lgb)
2567 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2571 void LyXText::Delete(BufferView * bview)
2573 // this is a very easy implementation
2575 LyXCursor old_cursor = cursor;
2576 int const old_cur_par_id = old_cursor.par()->id();
2577 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2578 old_cursor.par()->previous()->id() : 0;
2580 // just move to the right
2583 // CHECK Look at the comment here.
2584 // This check is not very good...
2585 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2586 // and that can very well delete the par or par->previous in
2587 // old_cursor. Will a solution where we compare paragraph id's
2589 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2590 == old_cur_par_prev_id
2591 && cursor.par()->id() != old_cur_par_id) {
2592 // delete-empty-paragraph-mechanism has done it
2596 // if you had success make a backspace
2597 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2598 LyXCursor tmpcursor = cursor;
2599 // to make sure undo gets the right cursor position
2600 cursor = old_cursor;
2601 setUndo(bview, Undo::DELETE,
2602 cursor.par(), cursor.par()->next());
2609 void LyXText::backspace(BufferView * bview)
2611 // Get the font that is used to calculate the baselineskip
2612 Paragraph::size_type lastpos = cursor.par()->size();
2613 LyXFont rawparfont =
2614 cursor.par()->getFontSettings(bview->buffer()->params,
2617 if (cursor.pos() == 0) {
2618 // The cursor is at the beginning of a paragraph,
2619 // so the the backspace will collapse two paragraphs into one.
2621 // we may paste some paragraphs
2623 // is it an empty paragraph?
2626 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2627 // This is an empty paragraph and we delete it just by moving the cursor one step
2628 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2629 // of the paragraph.
2631 if (cursor.par()->previous()) {
2632 Paragraph * tmppar = cursor.par()->previous();
2633 if (cursor.par()->getLayout() == tmppar->getLayout()
2634 && cursor.par()->getAlign() == tmppar->getAlign()) {
2635 // Inherit bottom DTD from the paragraph below.
2636 // (the one we are deleting)
2637 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2638 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2639 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2644 // the layout things can change the height of a row !
2645 int const tmpheight = cursor.row()->height();
2646 setHeightOfRow(bview, cursor.row());
2647 if (cursor.row()->height() != tmpheight) {
2648 refresh_y = cursor.y() - cursor.row()->baseline();
2649 refresh_row = cursor.row();
2650 status(bview, LyXText::NEED_MORE_REFRESH);
2656 if (cursor.par()->previous()) {
2657 setUndo(bview, Undo::DELETE,
2658 cursor.par()->previous(), cursor.par()->next());
2661 Paragraph * tmppar = cursor.par();
2662 Row * tmprow = cursor.row();
2664 // We used to do cursorLeftIntern() here, but it is
2665 // not a good idea since it triggers the auto-delete
2666 // mechanism. So we do a cursorLeftIntern()-lite,
2667 // without the dreaded mechanism. (JMarc)
2668 if (cursor.par()->previous()) {
2669 // steps into the above paragraph.
2670 setCursorIntern(bview, cursor.par()->previous(),
2671 cursor.par()->previous()->size(),
2675 /* Pasting is not allowed, if the paragraphs have different
2676 layout. I think it is a real bug of all other
2677 word processors to allow it. It confuses the user.
2678 Even so with a footnote paragraph and a non-footnote
2679 paragraph. I will not allow pasting in this case,
2680 because the user would be confused if the footnote behaves
2681 different wether it is open or closed.
2683 Correction: Pasting is always allowed with standard-layout
2685 if (cursor.par() != tmppar
2686 && (cursor.par()->getLayout() == tmppar->getLayout()
2687 || tmppar->getLayout() == 0 /*standard*/)
2688 && cursor.par()->getAlign() == tmppar->getAlign())
2690 removeParagraph(tmprow);
2692 cursor.par()->pasteParagraph(bview->buffer()->params);
2694 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2695 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2696 // strangely enough it seems that commenting out the line above removes
2697 // most or all of the segfaults. I will however also try to move the
2698 // two Remove... lines in front of the PasteParagraph too.
2701 cursor.pos(cursor.pos() - 1);
2703 status(bview, LyXText::NEED_MORE_REFRESH);
2704 refresh_row = cursor.row();
2705 refresh_y = cursor.y() - cursor.row()->baseline();
2707 // remove the lost paragraph
2708 // This one is not safe, since the paragraph that the tmprow and the
2709 // following rows belong to has been deleted by the PasteParagraph
2710 // above. The question is... could this be moved in front of the
2712 //RemoveParagraph(tmprow);
2713 //RemoveRow(tmprow);
2715 // This rebuilds the rows.
2716 appendParagraph(bview, cursor.row());
2717 updateCounters(bview, cursor.row());
2719 // the row may have changed, block, hfills etc.
2720 setCursor(bview, cursor.par(), cursor.pos(), false);
2723 /* this is the code for a normal backspace, not pasting
2725 setUndo(bview, Undo::DELETE,
2726 cursor.par(), cursor.par()->next());
2727 // We used to do cursorLeftIntern() here, but it is
2728 // not a good idea since it triggers the auto-delete
2729 // mechanism. So we do a cursorLeftIntern()-lite,
2730 // without the dreaded mechanism. (JMarc)
2731 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2732 false, cursor.boundary());
2734 // some insets are undeletable here
2735 if (cursor.par()->isInset(cursor.pos())) {
2736 if (!cursor.par()->getInset(cursor.pos())->deletable())
2738 // force complete redo when erasing display insets
2739 // this is a cruel method but safe..... Matthias
2740 if (cursor.par()->getInset(cursor.pos())->display() ||
2741 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2742 cursor.par()->erase(cursor.pos());
2743 redoParagraph(bview);
2748 Row * row = cursor.row();
2749 int y = cursor.y() - row->baseline();
2750 Paragraph::size_type z;
2751 /* remember that a space at the end of a row doesnt count
2752 * when calculating the fill */
2753 if (cursor.pos() < rowLast(row) ||
2754 !cursor.par()->isLineSeparator(cursor.pos())) {
2755 row->fill(row->fill() + singleWidth(bview,
2760 /* some special code when deleting a newline. This is similar
2761 * to the behavior when pasting paragraphs */
2762 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2763 cursor.par()->erase(cursor.pos());
2764 // refresh the positions
2766 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2767 tmprow = tmprow->next();
2768 tmprow->pos(tmprow->pos() - 1);
2770 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2771 cursor.pos(cursor.pos() - 1);
2773 if (cursor.pos() < cursor.par()->size()
2774 && !cursor.par()->isSeparator(cursor.pos())) {
2775 cursor.par()->insertChar(cursor.pos(), ' ');
2776 setCharFont(bview->buffer(), cursor.par(),
2777 cursor.pos(), current_font);
2778 // refresh the positions
2780 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2781 tmprow = tmprow->next();
2782 tmprow->pos(tmprow->pos() + 1);
2786 cursor.par()->erase(cursor.pos());
2788 // refresh the positions
2790 while (tmprow->next()
2791 && tmprow->next()->par() == row->par()) {
2792 tmprow = tmprow->next();
2793 tmprow->pos(tmprow->pos() - 1);
2796 // delete newlines at the beginning of paragraphs
2797 while (cursor.par()->size() &&
2798 cursor.par()->isNewline(cursor.pos()) &&
2799 cursor.pos() == beginningOfMainBody(bview->buffer(),
2801 cursor.par()->erase(cursor.pos());
2802 // refresh the positions
2804 while (tmprow->next() &&
2805 tmprow->next()->par() == row->par()) {
2806 tmprow = tmprow->next();
2807 tmprow->pos(tmprow->pos() - 1);
2812 // is there a break one row above
2813 if (row->previous() && row->previous()->par() == row->par()) {
2814 z = nextBreakPoint(bview, row->previous(),
2816 if (z >= row->pos()) {
2819 Row * tmprow = row->previous();
2821 // maybe the current row is now empty
2822 if (row->pos() >= row->par()->size()) {
2827 breakAgainOneRow(bview, row);
2828 if (row->next() && row->next()->par() == row->par())
2829 need_break_row = row->next();
2834 // set the dimensions of the row above
2835 y -= tmprow->height();
2836 tmprow->fill(fill(bview, tmprow,
2838 setHeightOfRow(bview, tmprow);
2841 refresh_row = tmprow;
2842 status(bview, LyXText::NEED_MORE_REFRESH);
2843 setCursor(bview, cursor.par(), cursor.pos(),
2844 false, cursor.boundary());
2845 //current_font = rawtmpfont;
2846 //real_current_font = realtmpfont;
2847 // check, whether the last character's font has changed.
2849 cursor.par()->getFontSettings(bview->buffer()->params,
2850 cursor.par()->size() - 1))
2851 redoHeightOfParagraph(bview, cursor);
2856 // break the cursor row again
2857 if (row->next() && row->next()->par() == row->par() &&
2858 (rowLast(row) == row->par()->size() - 1 ||
2859 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2861 /* it can happen that a paragraph loses one row
2862 * without a real breakup. This is when a word
2863 * is to long to be broken. Well, I don t care this
2865 if (rowLast(row) == row->par()->size() - 1)
2866 removeRow(row->next());
2870 status(bview, LyXText::NEED_MORE_REFRESH);
2872 breakAgainOneRow(bview, row);
2873 // will the cursor be in another row now?
2874 if (row->next() && row->next()->par() == row->par() &&
2875 rowLast(row) <= cursor.pos()) {
2877 breakAgainOneRow(bview, row);
2880 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2882 if (row->next() && row->next()->par() == row->par())
2883 need_break_row = row->next();
2887 // set the dimensions of the row
2888 row->fill(fill(bview, row, workWidth(bview)));
2889 int const tmpheight = row->height();
2890 setHeightOfRow(bview, row);
2891 if (tmpheight == row->height())
2892 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2894 status(bview, LyXText::NEED_MORE_REFRESH);
2897 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2901 // current_font = rawtmpfont;
2902 // real_current_font = realtmpfont;
2904 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2905 != cursor.boundary())
2906 setCursor(bview, cursor.par(), cursor.pos(), false,
2907 !cursor.boundary());
2909 lastpos = cursor.par()->size();
2910 if (cursor.pos() == lastpos)
2911 setCurrentFont(bview);
2913 // check, whether the last characters font has changed.
2915 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2916 redoHeightOfParagraph(bview, cursor);
2918 // now the special right address boxes
2919 if (textclasslist.Style(bview->buffer()->params.textclass,
2920 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2921 redoDrawingOfParagraph(bview, cursor);
2927 bool LyXText::paintRowBackground(DrawRowParams & p)
2929 bool clear_area = true;
2931 LyXFont font(LyXFont::ALL_SANE);
2933 Paragraph::size_type const last = rowLastPrintable(p.row);
2935 if (!p.bv->screen()->forceClear() && last == p.row->pos()
2936 && p.row->par()->isInset(p.row->pos())) {
2937 inset = p.row->par()->getInset(p.row->pos());
2939 clear_area = inset->doClearArea();
2949 int const y = p.yo < 0 ? 0 : p.yo;
2950 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
2951 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
2958 int h = p.row->baseline() - inset->ascent(p.bv, font);
2960 // first clear the whole row above the inset!
2962 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
2965 // clear the space below the inset!
2966 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
2967 if ((p.row->height() - h) > 0) {
2968 p.pain->fillRectangle(p.xo, p.yo + h,
2969 p.width, p.row->height() - h, backgroundColor());
2972 // clear the space behind the inset, if needed
2973 if (!inset->display() && !inset->needFullRow()) {
2974 int const xp = int(p.x) + inset->width(p.bv, font);
2975 if (p.width - xp > 0) {
2976 p.pain->fillRectangle(xp, p.yo, p.width - xp,
2977 p.row->height(), backgroundColor());
2985 void LyXText::paintRowSelection(DrawRowParams & p)
2987 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
2989 // the current selection
2990 int const startx = selection.start.x();
2991 int const endx = selection.end.x();
2992 int const starty = selection.start.y();
2993 int const endy = selection.end.y();
2994 Row const * startrow = selection.start.row();
2995 Row const * endrow = selection.end.row();
2999 if (bidi_same_direction) {
3003 int h = row->height();
3005 if (startrow == row && endrow == row) {
3006 if (startx < endx) {
3009 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3013 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3015 } else if (startrow == row) {
3016 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3017 int const w = (is_rtl) ? startx : (p.width - startx);
3018 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3019 } else if (endrow == row) {
3020 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3021 int const w = (is_rtl) ? (p.width - endx) : endx;
3022 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3023 } else if (p.y > starty && p.y < endy) {
3024 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3027 } else if (startrow != row && endrow != row) {
3029 int h = row->height();
3030 if (p.y > starty && p.y < endy) {
3031 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3036 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3042 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3044 Buffer const * buffer = p.bv->buffer();
3045 Paragraph * par = row->par();
3046 Paragraph::size_type main_body = beginningOfMainBody(buffer, par);
3047 Paragraph::size_type const last = rowLastPrintable(row);
3049 for (Paragraph::size_type vpos = row->pos(); vpos <= last; ++vpos) {
3050 Paragraph::size_type pos = vis2log(vpos);
3051 float const old_tmpx = tmpx;
3052 if (main_body > 0 && pos == main_body - 1) {
3053 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
3055 LyXFont const lfont = getLabelFont(buffer, par);
3058 tmpx += p.label_hfill + lyxfont::width(layout.labelsep, lfont);
3060 if (par->isLineSeparator(main_body - 1))
3061 tmpx -= singleWidth(p.bv, par, main_body - 1);
3064 if (hfillExpansion(buffer, row, pos)) {
3065 tmpx += singleWidth(p.bv, par, pos);
3066 if (pos >= main_body)
3069 tmpx += p.label_hfill;
3072 else if (par->isSeparator(pos)) {
3073 tmpx += singleWidth(p.bv, par, pos);
3074 if (pos >= main_body)
3075 tmpx += p.separator;
3077 tmpx += singleWidth(p.bv, par, pos);
3080 if ((startrow != row || selection.start.pos() <= pos) &&
3081 (endrow != row || pos < selection.end.pos())) {
3082 // Here we do not use p.x as p.xo was added to p.x.
3083 p.pain->fillRectangle(int(old_tmpx), p.yo,
3084 int(tmpx - old_tmpx + 1),
3085 row->height(), LColor::selection);
3088 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3089 p.pain->fillRectangle(p.xo + int(tmpx),
3090 p.yo, int(p.bv->workWidth() - tmpx),
3091 row->height(), LColor::selection);
3097 void LyXText::paintRowAppendix(DrawRowParams & p)
3099 // FIXME: can be just p.width ?
3100 int const ww = p.bv->workWidth();
3101 Paragraph * firstpar = p.row->par();
3103 if (firstpar->params().appendix()) {
3104 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3105 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3110 void LyXText::paintRowDepthBar(DrawRowParams & p)
3112 Paragraph::depth_type const depth = p.row->par()->getDepth();
3117 Paragraph::depth_type prev_depth = 0;
3118 if (p.row->previous())
3119 prev_depth = p.row->previous()->par()->getDepth();
3120 Paragraph::depth_type next_depth = 0;
3122 next_depth = p.row->next()->par()->getDepth();
3124 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3125 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3126 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3128 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3130 int const w = LYX_PAPER_MARGIN / 5;
3132 if (i > prev_depth) {
3133 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3135 if (i > next_depth) {
3136 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3142 void LyXText::paintFirstRow(DrawRowParams & p)
3144 Paragraph * par = p.row->par();
3145 ParagraphParameters const & parparams = par->params();
3147 // start of appendix?
3148 if (parparams.startOfAppendix()) {
3149 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3154 // think about the margins
3155 if (!p.row->previous() && bv_owner)
3156 y_top += LYX_PAPER_MARGIN;
3158 // draw a top pagebreak
3159 if (parparams.pagebreakTop()) {
3160 int const y = p.yo + y_top + 2*defaultHeight();
3161 p.pain->line(0, y, p.width, y, LColor::pagebreak, Painter::line_onoffdash);
3168 pb_font.setColor(LColor::pagebreak).decSize();
3169 lyxfont::rectText(_("Page Break (top)"), pb_font, w, a, d);
3170 p.pain->rectText((p.width - w)/2, y + d,
3171 _("Page Break (top)"), pb_font,
3174 y_top += 3 * defaultHeight();
3178 if (parparams.spaceTop().kind() == VSpace::VFILL) {
3179 int const y1 = p.yo + y_top + 3 * defaultHeight();
3180 int const y2 = p.yo + 2 + y_top;
3182 p.pain->line(0, y1, LYX_PAPER_MARGIN, y1, LColor::vfillline);
3184 p.pain->line(0, y2, LYX_PAPER_MARGIN, y2, LColor::vfillline);
3186 int const x = LYX_PAPER_MARGIN / 2;
3188 p.pain->line(x, y2, x, y1, LColor::vfillline);
3190 y_top += 3 * defaultHeight();
3193 // think about user added space
3194 y_top += int(parparams.spaceTop().inPixels(p.bv));
3196 Buffer const * buffer = p.bv->buffer();
3198 LyXLayout const & layout =
3199 textclasslist.Style(buffer->params.textclass, par->getLayout());
3201 // think about the parskip
3202 // some parskips VERY EASY IMPLEMENTATION
3203 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3204 if (par->previous()) {
3205 if (layout.latextype == LATEX_PARAGRAPH
3206 && !par->getDepth()) {
3207 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3209 LyXLayout const & playout =
3210 textclasslist.Style(buffer->params.textclass,
3211 par->previous()->getLayout());
3212 if (playout.latextype == LATEX_PARAGRAPH
3213 && !par->previous()->getDepth()) {
3214 // is it right to use defskip here, too? (AS)
3215 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3221 int const ww = p.bv->workWidth();
3224 if (parparams.lineTop()) {
3225 LyXFont font(LyXFont::ALL_SANE);
3226 int const asc = lyxfont::ascent('x', getFont(buffer, par, 0));
3230 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3231 int const xp = static_cast<int>(inset_owner ? p.x : 0);
3232 p.pain->line(xp, p.yo + y_top, w, p.yo + y_top,
3233 LColor::topline, Painter::line_solid,
3234 Painter::line_thick);
3239 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3241 // should we print a label?
3242 if (layout.labeltype >= LABEL_STATIC
3243 && (layout.labeltype != LABEL_STATIC
3244 || layout.latextype != LATEX_ENVIRONMENT
3245 || par->isFirstInSequence())) {
3247 LyXFont font = getLabelFont(buffer, par);
3248 if (!par->getLabelstring().empty()) {
3250 string const str = par->getLabelstring();
3252 // this is special code for the chapter layout. This is
3253 // printed in an extra row and has a pagebreak at
3255 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3256 if (buffer->params.secnumdepth >= 0) {
3257 float spacing_val = 1.0;
3258 if (!parparams.spacing().isDefault()) {
3259 spacing_val = parparams.spacing().getValue();
3261 spacing_val = buffer->params.spacing.getValue();
3265 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3266 + int(layout.parsep) * defaultHeight();
3269 x = ww - leftMargin(p.bv, p.row) -
3270 lyxfont::width(str, font);
3273 p.pain->text(int(x), p.yo +
3274 p.yo + p.row->baseline() -
3275 p.row->ascent_of_text() - maxdesc,
3280 x = ww - leftMargin(p.bv, p.row)
3281 + lyxfont::width(layout.labelsep, font);
3283 x = p.x - lyxfont::width(layout.labelsep, font)
3284 - lyxfont::width(str, font);
3286 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3289 // the labels at the top of an environment.
3290 // More or less for bibliography
3291 } else if (par->isFirstInSequence() &&
3292 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3293 layout.labeltype == LABEL_BIBLIO ||
3294 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3295 LyXFont font = getLabelFont(buffer, par);
3296 if (!par->getLabelstring().empty()) {
3297 string const str = par->getLabelstring();
3298 float spacing_val = 1.0;
3299 if (!parparams.spacing().isDefault()) {
3300 spacing_val = parparams.spacing().getValue();
3302 spacing_val = buffer->params.spacing.getValue();
3306 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3307 + (layout.labelbottomsep * defaultHeight()));
3310 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3311 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3312 + ww - rightMargin(buffer, p.row) ) / 2;
3313 x -= lyxfont::width(str, font) / 2;
3314 } else if (is_rtl) {
3315 x = ww - leftMargin(p.bv, p.row) -
3316 lyxfont::width(str, font);
3318 p.pain->text(int(x), p.yo + p.row->baseline()
3319 - p.row->ascent_of_text() - maxdesc,
3324 if (layout.labeltype == LABEL_BIBLIO && par->bibkey) {
3325 LyXFont font = getLayoutFont(buffer, par);
3328 x = ww - leftMargin(p.bv, p.row)
3329 + lyxfont::width(layout.labelsep, font);
3331 x = p.x - lyxfont::width(layout.labelsep, font)
3332 - par->bibkey->width(p.bv, font);
3334 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3339 void LyXText::paintLastRow(DrawRowParams & p)
3341 Paragraph * par = p.row->par();
3342 ParagraphParameters const & parparams = par->params();
3343 int y_bottom = p.row->height();
3345 // think about the margins
3346 if (!p.row->next() && bv_owner)
3347 y_bottom -= LYX_PAPER_MARGIN;
3349 int const ww = p.bv->workWidth();
3351 // draw a bottom pagebreak
3352 if (parparams.pagebreakBottom()) {
3354 pb_font.setColor(LColor::pagebreak).decSize();
3355 int const y = p.yo + y_bottom - 2 * defaultHeight();
3357 p.pain->line(0, y, ww, y, LColor::pagebreak, Painter::line_onoffdash);
3362 lyxfont::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3363 p.pain->rectText((ww - w) / 2, y + d,
3364 _("Page Break (bottom)"),
3365 pb_font, backgroundColor(), backgroundColor());
3367 y_bottom -= 3 * defaultHeight();
3370 // draw a vfill bottom
3371 if (parparams.spaceBottom().kind() == VSpace::VFILL) {
3372 int const x = LYX_PAPER_MARGIN / 2;
3373 int const x2 = LYX_PAPER_MARGIN;
3374 int const y = p.yo + y_bottom - 3 * defaultHeight();
3375 int const y2 = p.yo + y_bottom - 2;
3377 p.pain->line(0, y, x2, y, LColor::vfillline);
3378 p.pain->line(0, y2, x2, y2, LColor::vfillline);
3379 p.pain->line(x, y, x, y2, LColor::vfillline);
3381 y_bottom -= 3 * defaultHeight();
3384 // think about user added space
3385 y_bottom -= int(parparams.spaceBottom().inPixels(p.bv));
3387 Buffer const * buffer = p.bv->buffer();
3389 // draw a bottom line
3390 if (parparams.lineBottom()) {
3391 LyXFont font(LyXFont::ALL_SANE);
3392 int const asc = lyxfont::ascent('x',
3393 getFont(buffer, par,
3394 max(Paragraph::size_type(0), par->size() - 1)));
3398 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3399 int const xp = static_cast<int>(inset_owner ? p.x : 0);
3400 int const y = p.yo + y_bottom;
3401 p.pain->line(xp, y, w, y, LColor::topline, Painter::line_solid,
3402 Painter::line_thick);
3407 Paragraph::size_type const last = rowLastPrintable(p.row);
3408 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3409 int const endlabel = par->getEndLabel(buffer->params);
3414 case END_LABEL_FILLED_BOX:
3416 LyXFont const font = getFont(buffer, par, last);
3417 int const size = int(0.75 * lyxfont::maxAscent(font));
3418 int const y = (p.yo + p.row->baseline()) - size;
3419 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3421 if (p.row->fill() <= size)
3422 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3424 if (endlabel == END_LABEL_BOX) {
3425 p.pain->line(x, y, x, y + size, LColor::eolmarker);
3426 p.pain->line(x + size, y, x + size , y + size, LColor::eolmarker);
3427 p.pain->line(x, y, x + size, y, LColor::eolmarker);
3428 p.pain->line(x, y + size, x + size, y + size, LColor::eolmarker);
3430 p.pain->fillRectangle(x, y, size, size, LColor::eolmarker);
3434 case END_LABEL_STATIC:
3436 LyXFont font(LyXFont::ALL_SANE);
3437 LyXTextClass::LayoutList::size_type layout = par->getLayout();
3438 string const str = textclasslist.
3439 Style(buffer->params.textclass, layout).endlabelstring();
3440 font = getLabelFont(buffer, par);
3441 int const x = is_rtl ?
3442 int(p.x) - lyxfont::width(str, font)
3443 : ww - rightMargin(buffer, p.row) - p.row->fill();
3444 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3447 case END_LABEL_NO_LABEL:
3452 void LyXText::paintRowText(DrawRowParams & p)
3454 Paragraph * par = p.row->par();
3455 Buffer const * buffer = p.bv->buffer();
3457 Paragraph::size_type const last = rowLastPrintable(p.row);
3458 Paragraph::size_type main_body =
3459 beginningOfMainBody(buffer, par);
3460 if (main_body > 0 &&
3461 (main_body - 1 > last ||
3462 !par->isLineSeparator(main_body - 1))) {
3466 LyXLayout const & layout =
3467 textclasslist.Style(buffer->params.textclass, par->getLayout());
3469 Paragraph::size_type vpos = p.row->pos();
3470 while (vpos <= last) {
3471 Paragraph::size_type pos = vis2log(vpos);
3472 if (main_body > 0 && pos == main_body - 1) {
3473 int const lwidth = lyxfont::width(layout.labelsep,
3474 getLabelFont(buffer, par));
3476 p.x += p.label_hfill + lwidth
3477 - singleWidth(p.bv, par, main_body - 1);
3480 if (par->isHfill(pos)) {
3483 int const y0 = p.yo + p.row->baseline();
3484 int const y1 = y0 - defaultHeight() / 2;
3486 p.pain->line(int(p.x), y1, int(p.x), y0,
3489 if (hfillExpansion(buffer, p.row, pos)) {
3490 int const y2 = (y0 + y1) / 2;
3492 if (pos >= main_body) {
3493 p.pain->line(int(p.x), y2,
3494 int(p.x + p.hfill), y2,
3496 Painter::line_onoffdash);
3499 p.pain->line(int(p.x), y2,
3500 int(p.x + p.label_hfill), y2,
3502 Painter::line_onoffdash);
3503 p.x += p.label_hfill;
3505 p.pain->line(int(p.x), y1,
3511 } else if (par->isSeparator(pos)) {
3512 p.x += singleWidth(p.bv, par, pos);
3513 if (pos >= main_body)
3517 draw(p.bv, p.row, vpos, p.yo, p.x, p.cleared);
3523 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3524 Row * row, int y, bool cleared)
3526 if (row->height() <= 0) {
3527 lyxerr << "LYX_ERROR: row.height: "
3528 << row->height() << endl;
3534 // set up drawing parameters
3536 p.pain = &bv->painter();
3540 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3541 if (inset_owner && (p.x < 0))
3545 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3546 p.cleared = cleared;
3550 // clear to background if necessary
3551 p.cleared = paintRowBackground(p);
3553 // paint the selection background
3554 if (selection.set()) {
3555 paintRowSelection(p);
3558 // vertical lines for appendix
3559 paintRowAppendix(p);
3561 // environment depth brackets
3562 paintRowDepthBar(p);
3564 // draw any stuff wanted for a first row of a paragraph
3569 // draw any stuff wanted for the last row of a paragraph
3570 if (!row->next() || (row->next()->par() != row->par())) {
3579 int LyXText::defaultHeight() const
3581 LyXFont font(LyXFont::ALL_SANE);
3582 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3586 /* returns the column near the specified x-coordinate of the row
3587 * x is set to the real beginning of this column */
3588 Paragraph::size_type
3589 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3590 bool & boundary) const
3593 float fill_separator;
3595 float fill_label_hfill;
3597 prepareToPrint(bview, row, tmpx, fill_separator,
3598 fill_hfill, fill_label_hfill);
3600 Paragraph::size_type vc = row->pos();
3601 Paragraph::size_type last = rowLastPrintable(row);
3602 Paragraph::size_type c = 0;
3603 LyXLayout const & layout =
3604 textclasslist.Style(bview->buffer()->params.textclass,
3605 row->par()->getLayout());
3606 bool left_side = false;
3608 Paragraph::size_type
3609 main_body = beginningOfMainBody(bview->buffer(), row->par());
3610 float last_tmpx = tmpx;
3612 if (main_body > 0 &&
3613 (main_body - 1 > last ||
3614 !row->par()->isLineSeparator(main_body - 1)))
3617 while (vc <= last && tmpx <= x) {
3620 if (main_body > 0 && c == main_body-1) {
3621 tmpx += fill_label_hfill +
3622 lyxfont::width(layout.labelsep,
3623 getLabelFont(bview->buffer(), row->par()));
3624 if (row->par()->isLineSeparator(main_body - 1))
3625 tmpx -= singleWidth(bview, row->par(), main_body-1);
3628 if (hfillExpansion(bview->buffer(), row, c)) {
3629 x += singleWidth(bview, row->par(), c);
3633 tmpx += fill_label_hfill;
3635 else if (row->par()->isSeparator(c)) {
3636 tmpx += singleWidth(bview, row->par(), c);
3638 tmpx+= fill_separator;
3640 tmpx += singleWidth(bview, row->par(), c);
3644 if ((tmpx + last_tmpx) / 2 > x) {
3649 if (vc > last + 1) // This shouldn't happen.
3653 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3654 // some speedup if rtl_support=false
3655 && (!row->next() || row->next()->par() != row->par());
3656 bool const rtl = (lastrow)
3657 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3658 : false; // If lastrow is false, we don't need to compute
3659 // the value of rtl.
3661 if (row->pos() > last) // Row is empty?
3664 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3665 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3667 else if (vc == row->pos()) {
3669 if (bidi_level(c) % 2 == 1)
3672 c = vis2log(vc - 1);
3673 bool const rtl = (bidi_level(c) % 2 == 1);
3674 if (left_side == rtl) {
3676 boundary = isBoundary(bview->buffer(), row->par(), c);
3680 if (row->pos() <= last && c > last
3681 && row->par()->isNewline(last)) {
3682 if (bidi_level(last) % 2 == 0)
3683 tmpx -= singleWidth(bview, row->par(), last);
3685 tmpx += singleWidth(bview, row->par(), last);
3695 // returns pointer to a specified row
3696 Row * LyXText::getRow(Paragraph * par,
3697 Paragraph::size_type pos, int & y) const
3702 Row * tmprow = firstrow;
3705 // find the first row of the specified paragraph
3706 while (tmprow->next() && tmprow->par() != par) {
3707 y += tmprow->height();
3708 tmprow = tmprow->next();
3711 // now find the wanted row
3712 while (tmprow->pos() < pos
3714 && tmprow->next()->par() == par
3715 && tmprow->next()->pos() <= pos) {
3716 y += tmprow->height();
3717 tmprow = tmprow->next();