1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
18 #include "paragraph.h"
19 #include "support/textutils.h"
20 #include "insets/insetbib.h"
21 #include "insets/insettext.h"
22 #include "lyx_gui_misc.h"
24 #include "bufferparams.h"
33 #include "lyxscreen.h"
34 #include "bufferview_funcs.h"
35 #include "BufferView.h"
37 #include "ParagraphParameters.h"
46 int const LYX_PAPER_MARGIN = 20;
50 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
53 int LyXText::workWidth(BufferView * bview) const
56 return inset_owner->textWidth(bview);
58 return bview->workWidth();
62 int LyXText::getRealCursorX(BufferView * bview) const
65 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
66 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
71 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
72 Paragraph::size_type pos) const
74 if (!Encodings::is_arabic(c))
75 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
76 return c + (0xb0 - '0');
80 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
81 unsigned char next_char = ' ';
83 for (Paragraph::size_type i = pos+1; i < par->size(); ++i)
84 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
85 next_char = par->getChar(i);
89 if (Encodings::is_arabic(next_char)) {
90 if (Encodings::is_arabic(prev_char))
91 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
93 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
95 if (Encodings::is_arabic(prev_char))
96 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
98 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
102 // This is the comments that some of the warnings below refers to.
103 // There are some issues in this file and I don't think they are
104 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
105 // this is a problem that has been here almost from day one and that a
106 // larger userbase with differenct access patters triggers the bad
107 // behaviour. (segfaults.) What I think happen is: In several places
108 // we store the paragraph in the current cursor and then moves the
109 // cursor. This movement of the cursor will delete paragraph at the
110 // old position if it is now empty. This will make the temporary
111 // pointer to the old cursor paragraph invalid and dangerous to use.
112 // And is some cases this will trigger a segfault. I have marked some
113 // of the cases where this happens with a warning, but I am sure there
114 // are others in this file and in text2.C. There is also a note in
115 // Delete() that you should read. In Delete I store the paragraph->id
116 // instead of a pointer to the paragraph. I am pretty sure this faulty
117 // use of temporary pointers to paragraphs that might have gotten
118 // invalidated (through a cursor movement) before they are used, are
119 // the cause of the strange crashes we get reported often.
121 // It is very tiresom to change this code, especially when it is as
122 // hard to read as it is. Help to fix all the cases where this is done
123 // would be greately appreciated.
127 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
128 Paragraph::size_type pos) const
130 char const c = par->getChar(pos);
131 return singleWidth(bview, par, pos, c);
135 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
136 Paragraph::size_type pos, char c) const
138 LyXFont const font = getFont(bview->buffer(), par, pos);
140 // The most common case is handled first (Asger)
141 if (IsPrintable(c)) {
142 if (font.language()->RightToLeft()) {
143 if (font.language()->lang() == "arabic" &&
144 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
145 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
146 if (Encodings::IsComposeChar_arabic(c))
149 c = transformChar(c, par, pos);
150 else if (font.language()->lang() == "hebrew" &&
151 Encodings::IsComposeChar_hebrew(c))
154 return lyxfont::width(c, font);
156 } else if (IsHfillChar(c)) {
157 return 3; /* Because of the representation
158 * as vertical lines */
159 } else if (c == Paragraph::META_INSET) {
160 Inset * tmpinset = par->getInset(pos);
162 #if 0 // seems not to be needed, but ...
163 tmpinset->update(bview, font);
165 return tmpinset->width(bview, font);
169 } else if (IsSeparatorChar(c))
171 else if (IsNewlineChar(c))
173 return lyxfont::width(c, font);
177 // Returns the paragraph position of the last character in the specified row
178 Paragraph::size_type LyXText::rowLast(Row const * row) const
180 if (row->next() == 0)
181 return row->par()->size() - 1;
182 else if (row->next()->par() != row->par())
183 return row->par()->size() - 1;
185 return row->next()->pos() - 1;
189 Paragraph::size_type LyXText::rowLastPrintable(Row const * row) const
191 Paragraph::size_type const last = rowLast(row);
192 if (last >= row->pos()
194 && row->next()->par() == row->par()
195 && row->par()->isSeparator(last))
202 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
204 bidi_same_direction = true;
205 if (!lyxrc.rtl_support) {
210 bidi_start = row->pos();
211 bidi_end = rowLastPrintable(row);
213 if (bidi_start > bidi_end) {
218 if (bidi_end + 2 - bidi_start >
219 static_cast<Paragraph::size_type>(log2vis_list.size())) {
220 Paragraph::size_type new_size =
221 (bidi_end + 2 - bidi_start < 500) ?
222 500 : 2 * (bidi_end + 2 - bidi_start);
223 log2vis_list.resize(new_size);
224 vis2log_list.resize(new_size);
225 bidi_levels.resize(new_size);
228 vis2log_list[bidi_end + 1 - bidi_start] = -1;
229 log2vis_list[bidi_end + 1 - bidi_start] = -1;
231 Paragraph::size_type stack[2];
233 row->par()->getParLanguage(buf->params)->RightToLeft();
237 Paragraph::size_type const main_body =
238 beginningOfMainBody(buf, row->par());
240 for (Paragraph::size_type lpos = bidi_start;
241 lpos <= bidi_end; ++lpos) {
242 bool is_space = row->par()->isLineSeparator(lpos);
243 Paragraph::size_type const pos =
244 (is_space && lpos + 1 <= bidi_end &&
245 !row->par()->isLineSeparator(lpos + 1) &&
246 !row->par()->isNewline(lpos + 1))
248 LyXFont font = row->par()->getFontSettings(buf->params, pos);
249 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
250 font.number() == LyXFont::ON &&
251 row->par()->getFontSettings(buf->params, lpos-1).number()
253 font = row->par()->getFontSettings(buf->params, lpos);
258 bool new_rtl = font.isVisibleRightToLeft();
259 bool new_rtl0 = font.isRightToLeft();
262 if (lpos == main_body - 1
263 && row->pos() < main_body - 1
265 new_level = (rtl_par) ? 1 : 0;
266 new_rtl = new_rtl0 = rtl_par;
268 new_level = (new_rtl) ? 1 : 2;
270 new_level = (rtl_par) ? 2 : 0;
272 if (is_space && new_level >= level) {
278 int new_level2 = new_level;
280 if (level == new_level && rtl0 != new_rtl0) {
282 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
283 } else if (level < new_level) {
284 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
285 if (new_level > rtl_par)
286 bidi_same_direction = false;
288 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
291 bidi_levels[lpos - bidi_start] = new_level;
293 while (level > new_level2) {
294 Paragraph::size_type old_lpos =
296 int delta = lpos - old_lpos - 1;
299 log2vis_list[lpos - bidi_start] += delta;
300 log2vis_list[old_lpos - bidi_start] += delta;
302 while (level < new_level)
303 stack[level++] = lpos;
307 Paragraph::size_type const old_lpos = stack[--level];
308 int delta = bidi_end - old_lpos;
311 log2vis_list[old_lpos - bidi_start] += delta;
314 Paragraph::size_type vpos = bidi_start - 1;
315 for (Paragraph::size_type lpos = bidi_start;
316 lpos <= bidi_end; ++lpos) {
317 vpos += log2vis_list[lpos - bidi_start];
318 vis2log_list[vpos - bidi_start] = lpos;
319 log2vis_list[lpos - bidi_start] = vpos;
324 // This method requires a previous call to ComputeBidiTables()
325 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
326 Paragraph::size_type pos) const
328 if (!lyxrc.rtl_support || pos == 0)
331 if (!bidi_InRange(pos - 1)) {
332 /// This can happen if pos is the first char of a row.
333 /// Returning false in this case is incorrect!
337 bool const rtl = bidi_level(pos - 1) % 2;
338 bool const rtl2 = bidi_InRange(pos)
339 ? bidi_level(pos) % 2
340 : par->isRightToLeftPar(buf->params);
345 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
346 Paragraph::size_type pos,
347 LyXFont const & font) const
349 if (!lyxrc.rtl_support)
350 return false; // This is just for speedup
352 bool const rtl = font.isVisibleRightToLeft();
353 bool const rtl2 = bidi_InRange(pos)
354 ? bidi_level(pos) % 2
355 : par->isRightToLeftPar(buf->params);
360 void LyXText::draw(BufferView * bview, Row const * row,
361 Paragraph::size_type & vpos,
362 int offset, float & x, bool cleared)
364 Painter & pain = bview->painter();
366 Paragraph::size_type pos = vis2log(vpos);
367 char c = row->par()->getChar(pos);
370 if (IsNewlineChar(c)) {
372 // Draw end-of-line marker
373 LyXFont const font = getFont(bview->buffer(), row->par(), pos);
374 int const wid = lyxfont::width('n', font);
375 int const asc = lyxfont::maxAscent(font);
376 int const y = offset + row->baseline();
380 if (bidi_level(pos) % 2 == 0) {
381 xp[0] = int(x + wid * 0.375);
382 yp[0] = int(y - 0.875 * asc * 0.75);
385 yp[1] = int(y - 0.500 * asc * 0.75);
387 xp[2] = int(x + wid * 0.375);
388 yp[2] = int(y - 0.125 * asc * 0.75);
390 pain.lines(xp, yp, 3, LColor::eolmarker);
393 yp[0] = int(y - 0.500 * asc * 0.75);
395 xp[1] = int(x + wid);
396 yp[1] = int(y - 0.500 * asc * 0.75);
398 xp[2] = int(x + wid);
399 yp[2] = int(y - asc * 0.75);
401 pain.lines(xp, yp, 3, LColor::eolmarker);
403 xp[0] = int(x + wid * 0.625);
404 yp[0] = int(y - 0.875 * asc * 0.75);
406 xp[1] = int(x + wid);
407 yp[1] = int(y - 0.500 * asc * 0.75);
409 xp[2] = int(x + wid * 0.625);
410 yp[2] = int(y - 0.125 * asc * 0.75);
412 pain.lines(xp, yp, 3, LColor::eolmarker);
414 xp[0] = int(x + wid);
415 yp[0] = int(y - 0.500 * asc * 0.75);
418 yp[1] = int(y - 0.500 * asc * 0.75);
421 yp[2] = int(y - asc * 0.75);
423 pain.lines(xp, yp, 3, LColor::eolmarker);
429 LyXFont font = getFont(bview->buffer(), row->par(), pos);
430 LyXFont font2 = font;
432 if (c == Paragraph::META_INSET) {
433 Inset * tmpinset = row->par()->getInset(pos);
435 tmpinset->update(bview, font, false);
436 tmpinset->draw(bview, font, offset+row->baseline(), x,
438 #ifdef SEEMS_TO_BE_NOT_NEEDED
439 if (status == CHANGED_IN_DRAW) {
440 UpdateInset(bview, tmpinset);
441 status = CHANGED_IN_DRAW;
447 if (lyxrc.mark_foreign_language &&
448 font.language() != bview->buffer()->params.language) {
449 int const y = offset + row->height() - 1;
450 pain.line(int(tmpx), y, int(x), y,
457 /* usual characters, no insets */
459 // Collect character that we can draw in one command
461 // This is dirty, but fast. Notice that it will never be too small.
462 // For the record, I'll note that Microsoft Word has a limit
463 // of 768 here. We have none :-) (Asger)
464 // Ok. I am the first to admit that the use of std::string will be
465 // a tiny bit slower than using a POD char array. However, I claim
466 // that this slowdown is so small that it is close to inperceptive.
467 // So IMHO we should go with the easier and clearer implementation.
468 // And even if 1024 is a large number here it might overflow, string
469 // will only overflow if the machine is out of memory...
470 static string textstring;
474 Paragraph::size_type const last = rowLastPrintable(row);
476 if (font.language()->lang() == "hebrew") {
477 if (Encodings::IsComposeChar_hebrew(c)) {
478 int const width = lyxfont::width(c, font2);
480 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
481 c = row->par()->getChar(i);
482 if (!Encodings::IsComposeChar_hebrew(c)) {
483 if (IsPrintableNonspace(c)) {
488 dx = (c == 'ø' || c == 'ã') // dalet / resh
489 ? width2 - width : (width2 - width) / 2;
495 pain.text(int(x) + dx, offset + row->baseline(),
498 while (vpos <= last &&
499 (pos = vis2log(vpos)) >= 0
500 && IsPrintableNonspace(c = row->par()->getChar(pos))
501 && !Encodings::IsComposeChar_hebrew(c)
502 && font2 == getFont(bview->buffer(), row->par(), pos)) {
506 // Draw text and set the new x position
507 pain.text(int(x), offset + row->baseline(),
509 x += lyxfont::width(textstring, font);
511 } else if (font.language()->lang() == "arabic" &&
512 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
513 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
514 if (Encodings::IsComposeChar_arabic(c)) {
515 c = transformChar(c, row->par(), pos);
517 int const width = lyxfont::width(c, font2);
519 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
520 c = row->par()->getChar(i);
521 if (!Encodings::IsComposeChar_arabic(c)) {
522 if (IsPrintableNonspace(c)) {
527 dx = (width2 - width) / 2;
533 pain.text(int(x) + dx, offset + row->baseline(),
536 textstring = transformChar(c, row->par(), pos);
537 while (vpos <= last &&
538 (pos = vis2log(vpos)) >= 0
539 && IsPrintableNonspace(c = row->par()->getChar(pos))
540 && !Encodings::IsComposeChar_arabic(c)
541 && font2 == getFont(bview->buffer(), row->par(), pos)) {
542 c = transformChar(c, row->par(), pos);
546 // Draw text and set the new x position
547 pain.text(int(x), offset + row->baseline(),
549 x += lyxfont::width(textstring, font);
552 while (vpos <= last &&
553 (pos = vis2log(vpos)) >= 0
554 && IsPrintableNonspace(c = row->par()->getChar(pos))
555 && font2 == getFont(bview->buffer(), row->par(), pos)) {
559 // Draw text and set the new x position
560 pain.text(int(x), offset + row->baseline(), textstring, font);
561 x += lyxfont::width(textstring, font);
564 if (lyxrc.mark_foreign_language &&
565 font.language() != bview->buffer()->params.language) {
566 int const y = offset + row->height() - 1;
567 pain.line(int(tmpx), y, int(x), y,
571 // If we want ulem.sty support, drawing
572 // routines should go here. (Asger)
573 // Why shouldn't LyXFont::drawText handle it internally?
577 // Returns the left beginning of the text.
578 // This information cannot be taken from the layouts-objekt, because in
579 // LaTeX the beginning of the text fits in some cases (for example sections)
580 // exactly the label-width.
581 int LyXText::leftMargin(BufferView * bview, Row const * row) const
583 LyXLayout const & layout =
584 textclasslist.Style(bview->buffer()->params.textclass,
585 row->par()->getLayout());
587 string parindent = layout.parindent;
589 int x = LYX_PAPER_MARGIN;
591 x += lyxfont::signedWidth(textclasslist
592 .TextClass(bview->buffer()->params.textclass)
595 .TextClass(bview->buffer()->params.textclass)
598 // this is the way, LyX handles the LaTeX-Environments.
599 // I have had this idea very late, so it seems to be a
600 // later added hack and this is true
601 if (!row->par()->getDepth()) {
602 if (!row->par()->getLayout()) {
603 // find the previous same level paragraph
604 if (row->par()->previous()) {
605 Paragraph * newpar = row->par()
606 ->depthHook(row->par()->getDepth());
608 textclasslist.Style(bview->buffer()->params.textclass,
615 // find the next level paragraph
618 row->par()->outerHook();
620 // make a corresponding row. Needed to call LeftMargin()
622 // check wether it is a sufficent paragraph
625 .Style(bview->buffer()->params.textclass,
626 newpar->getLayout()).isEnvironment()) {
628 dummyrow.par(newpar);
629 dummyrow.pos(newpar->size());
630 x = leftMargin(bview, &dummyrow);
632 // this is no longer an error, because this function
633 // is used to clear impossible depths after changing
634 // a layout. Since there is always a redo,
635 // LeftMargin() is always called
636 row->par()->params().depth(0);
639 if (newpar && !row->par()->getLayout()) {
640 if (newpar->params().noindent())
643 parindent = textclasslist
644 .Style(bview->buffer()->params.textclass,
645 newpar->getLayout()).parindent;
650 LyXFont const labelfont = getFont(bview->buffer(), row->par(), -2);
651 switch (layout.margintype) {
653 if (!layout.leftmargin.empty()) {
654 x += lyxfont::signedWidth(layout.leftmargin,
656 .TextClass(bview->buffer()->params.
660 if (!row->par()->getLabelstring().empty()) {
661 x += lyxfont::signedWidth(layout.labelindent,
663 x += lyxfont::width(row->par()->getLabelstring(),
665 x += lyxfont::width(layout.labelsep, labelfont);
669 x += lyxfont::signedWidth(layout.labelindent, labelfont);
670 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
671 if (!row->par()->getLabelWidthString().empty()) {
672 x += lyxfont::width(row->par()->getLabelWidthString(),
674 x += lyxfont::width(layout.labelsep, labelfont);
679 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
680 / (row->par()->getDepth() + 4);
682 case MARGIN_FIRST_DYNAMIC:
683 if (layout.labeltype == LABEL_MANUAL) {
684 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
685 x += lyxfont::signedWidth(layout.leftmargin,
688 x += lyxfont::signedWidth(layout.labelindent,
691 } else if (row->pos()
692 // Special case to fix problems with
694 || (layout.labeltype == LABEL_STATIC
695 && layout.latextype == LATEX_ENVIRONMENT
696 && ! row->par()->isFirstInSequence())) {
697 x += lyxfont::signedWidth(layout.leftmargin,
699 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
700 && layout.labeltype != LABEL_BIBLIO
701 && layout.labeltype !=
702 LABEL_CENTERED_TOP_ENVIRONMENT) {
703 x += lyxfont::signedWidth(layout.labelindent,
705 x += lyxfont::width(layout.labelsep, labelfont);
706 x += lyxfont::width(row->par()->getLabelstring(),
711 case MARGIN_RIGHT_ADDRESS_BOX:
713 // ok, a terrible hack. The left margin depends on the widest
714 // row in this paragraph. Do not care about footnotes, they
715 // are *NOT* allowed in the LaTeX realisation of this layout.
717 // find the first row of this paragraph
718 Row const * tmprow = row;
719 while (tmprow->previous()
720 && tmprow->previous()->par() == row->par())
721 tmprow = tmprow->previous();
723 int minfill = tmprow->fill();
724 while (tmprow->next() && tmprow->next()->par() == row->par()) {
725 tmprow = tmprow->next();
726 if (tmprow->fill() < minfill)
727 minfill = tmprow->fill();
730 x += lyxfont::signedWidth(layout.leftmargin,
732 .TextClass(bview->buffer()->params.textclass)
739 int align; // wrong type
741 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
742 align = layout.align;
744 align = row->par()->params().align();
746 // set the correct parindent
747 if (row->pos() == 0) {
748 if ((layout.labeltype == LABEL_NO_LABEL
749 || layout.labeltype == LABEL_TOP_ENVIRONMENT
750 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
751 || (layout.labeltype == LABEL_STATIC
752 && layout.latextype == LATEX_ENVIRONMENT
753 && ! row->par()->isFirstInSequence()))
754 && align == LYX_ALIGN_BLOCK
755 && !row->par()->params().noindent()
756 && (row->par()->layout ||
757 bview->buffer()->params.paragraph_separation ==
758 BufferParams::PARSEP_INDENT))
759 x += lyxfont::signedWidth(parindent,
761 .TextClass(bview->buffer()->params
764 else if (layout.labeltype == LABEL_BIBLIO) {
765 // ale970405 Right width for bibitems
766 x += bibitemMaxWidth(bview,textclasslist
767 .TextClass(bview->buffer()->params
776 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
778 LyXLayout const & layout =
779 textclasslist.Style(buf->params.textclass,
780 row->par()->getLayout());
782 int x = LYX_PAPER_MARGIN
783 + lyxfont::signedWidth(textclasslist
784 .TextClass(buf->params.textclass)
787 .TextClass(buf->params.textclass)
790 // this is the way, LyX handles the LaTeX-Environments.
791 // I have had this idea very late, so it seems to be a
792 // later added hack and this is true
793 if (row->par()->getDepth()) {
794 // find the next level paragraph
796 Paragraph * newpar = row->par();
799 newpar = newpar->previous();
801 && newpar->getDepth() >= row->par()->getDepth());
803 // make a corresponding row. Needed to call LeftMargin()
805 // check wether it is a sufficent paragraph
807 && textclasslist.Style(buf->params.textclass,
811 dummyrow.par(newpar);
813 x = rightMargin(buf, &dummyrow);
815 // this is no longer an error, because this function
816 // is used to clear impossible depths after changing
817 // a layout. Since there is always a redo,
818 // LeftMargin() is always called
819 row->par()->params().depth(0);
823 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
824 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
825 .TextClass(buf->params.textclass)
826 .defaultfont()) * 4 / (row->par()->getDepth()
832 int LyXText::labelEnd(BufferView * bview, Row const * row) const
834 if (textclasslist.Style(bview->buffer()->params.textclass,
835 row->par()->getLayout()).margintype
839 tmprow.pos(row->par()->size());
840 return leftMargin(bview, &tmprow); /* just the beginning
843 return 0; /* LabelEnd is only needed, if the
844 layout fills a flushleft
849 // get the next breakpoint in a given paragraph
851 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
853 Paragraph * par = row->par();
858 Paragraph::size_type const pos = row->pos();
861 // position of the last possible breakpoint
862 // -1 isn't a suitable value, but a flag
863 Paragraph::size_type last_separator = -1;
864 width -= rightMargin(bview->buffer(), row);
866 Paragraph::size_type const main_body =
867 beginningOfMainBody(bview->buffer(), par);
868 LyXLayout const & layout =
869 textclasslist.Style(bview->buffer()->params.textclass, par->getLayout());
870 Paragraph::size_type i = pos;
872 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
873 /* special code for right address boxes, only newlines count */
874 while (i < par->size()) {
875 if (par->isNewline(i)) {
877 i = par->size() - 1; // this means break
879 } else if (par->getChar(i) == Paragraph::META_INSET &&
880 par->getInset(i) && par->getInset(i)->display()){
881 par->getInset(i)->display(false);
886 // Last position is an invariant
887 Paragraph::size_type const last =
889 // this is the usual handling
890 int x = leftMargin(bview, row);
891 bool doitonetime = true;
892 while (doitonetime || ((x < width) && (i < last))) {
894 char const c = par->getChar(i);
895 if (IsNewlineChar(c)) {
897 x = width; // this means break
898 } else if (c == Paragraph::META_INSET &&
901 // check wether a Display() inset is
902 // valid here. if not, change it to
904 if (par->getInset(i)->display() &&
905 (layout.isCommand() ||
906 (layout.labeltype == LABEL_MANUAL
907 && i < beginningOfMainBody(bview->buffer(), par)))) {
908 // display istn't allowd
909 par->getInset(i)->display(false);
910 x += singleWidth(bview, par, i, c);
911 } else if (par->getInset(i)->display() ||
912 par->getInset(i)->needFullRow()) {
913 // So break the line here
917 if (IsLineSeparatorChar(par->getChar(i+1)))
920 last_separator = last; // to avoid extra rows
922 last_separator = i - 1;
923 x = width; // this means break
925 x += singleWidth(bview, par, i, c);
928 if (IsLineSeparatorChar(c))
930 x += singleWidth(bview, par, i, c);
933 if (i == main_body) {
934 x += lyxfont::width(layout.labelsep,
935 getFont(bview->buffer(), par, -2));
936 if (par->isLineSeparator(i - 1))
937 x-= singleWidth(bview, par, i - 1);
938 int left_margin = labelEnd(bview, row);
943 // end of paragraph is always a suitable separator
944 if (i == last && x < width)
948 // well, if last_separator is still 0, the line isn't breakable.
949 // don't care and cut simply at the end
950 if (last_separator < 0) {
954 // manual labels cannot be broken in LaTeX, do not care
955 if (main_body && last_separator < main_body)
956 last_separator = main_body - 1;
958 return last_separator;
962 // returns the minimum space a row needs on the screen in pixel
963 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
969 // get the pure distance
970 Paragraph::size_type const last = rowLastPrintable(row);
972 // special handling of the right address boxes
973 if (textclasslist.Style(bview->buffer()->params.textclass,
974 row->par()->getLayout()).margintype
975 == MARGIN_RIGHT_ADDRESS_BOX) {
976 int const tmpfill = row->fill();
977 row->fill(0); // the minfill in MarginLeft()
978 w = leftMargin(bview, row);
981 w = leftMargin(bview, row);
983 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
984 row->par()->getLayout());
985 Paragraph::size_type const main_body =
986 beginningOfMainBody(bview->buffer(), row->par());
987 Paragraph::size_type i = row->pos();
990 if (main_body > 0 && i == main_body) {
991 w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
992 if (row->par()->isLineSeparator(i - 1))
993 w -= singleWidth(bview, row->par(), i - 1);
994 int left_margin = labelEnd(bview, row);
998 w += singleWidth(bview, row->par(), i);
1001 if (main_body > 0 && main_body > last) {
1002 w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
1003 if (last >= 0 && row->par()->isLineSeparator(last))
1004 w -= singleWidth(bview, row->par(), last);
1005 int const left_margin = labelEnd(bview, row);
1006 if (w < left_margin)
1010 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1011 #ifdef WITH_WARNINGS
1012 #warning Please fix me (Jug!)
1022 // returns the minimum space a manual label needs on the screen in pixel
1023 int LyXText::labelFill(BufferView * bview, Row const * row) const
1025 Paragraph::size_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1026 // -1 because a label ends either with a space that is in the label,
1027 // or with the beginning of a footnote that is outside the label.
1029 // I don't understand this code in depth, but sometimes "last" is
1030 // less than 0 and this causes a crash. This fix seems to work
1031 // correctly, but I bet the real error is elsewhere. The bug is
1032 // triggered when you have an open footnote in a paragraph
1033 // environment with a manual label. (Asger)
1034 if (last < 0) last = 0;
1036 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1043 w += singleWidth(bview, row->par(), i);
1048 if (!row->par()->params().labelWidthString().empty()) {
1049 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1050 getFont(bview->buffer(), row->par(), -2)) - w,
1058 // returns the number of separators in the specified row. The separator
1059 // on the very last column doesnt count
1060 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1062 Paragraph::size_type const last = rowLast(row);
1063 Paragraph::size_type p =
1064 max(row->pos(), beginningOfMainBody(buf, row->par()));
1066 for (; p < last; ++p) {
1067 if (row->par()->isSeparator(p)) {
1075 // returns the number of hfills in the specified row. The LyX-Hfill is
1076 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1077 // ignored. This is *MUCH* more usefull than not to ignore!
1078 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1080 Paragraph::size_type const last = rowLast(row);
1081 Paragraph::size_type first = row->pos();
1082 if (first) { /* hfill *DO* count at the beginning
1084 while(first <= last && row->par()->isHfill(first))
1088 first = max(first, beginningOfMainBody(buf, row->par()));
1090 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1091 if (row->par()->isHfill(p)) {
1099 // like NumberOfHfills, but only those in the manual label!
1100 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1102 Paragraph::size_type last = rowLast(row);
1103 Paragraph::size_type first = row->pos();
1104 if (first) { /* hfill *DO* count at the beginning
1106 while(first < last && row->par()->isHfill(first))
1110 last = min(last, beginningOfMainBody(buf, row->par()));
1112 for (Paragraph::size_type p = first;
1113 p < last; ++p) { // last, because the end is ignored!
1114 if (row->par()->isHfill(p)) {
1122 // returns true, if a expansion is needed.
1123 // Rules are given by LaTeX
1124 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1125 Paragraph::size_type pos) const
1127 // by the way, is it a hfill?
1128 if (!row_ptr->par()->isHfill(pos))
1131 // at the end of a row it does not count
1132 if (pos >= rowLast(row_ptr))
1135 // at the beginning of a row it does not count, if it is not
1136 // the first row of a paragaph
1137 if (!row_ptr->pos())
1140 // in some labels it does not count
1141 if (textclasslist.Style(buf->params.textclass,
1142 row_ptr->par()->getLayout()).margintype
1144 && pos < beginningOfMainBody(buf, row_ptr->par()))
1147 // if there is anything between the first char of the row and
1148 // the sepcified position that is not a newline and not a hfill,
1149 // the hfill will count, otherwise not
1150 Paragraph::size_type i = row_ptr->pos();
1151 while (i < pos && (row_ptr->par()->isNewline(i)
1152 || row_ptr->par()->isHfill(i)))
1159 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1161 /* get the maximum ascent and the maximum descent */
1164 float layoutasc = 0;
1165 float layoutdesc = 0;
1168 Inset * tmpinset = 0;
1170 /* this must not happen before the currentrow for clear reasons.
1171 so the trick is just to set the current row onto this row */
1173 getRow(row_ptr->par(), row_ptr->pos(), unused_y);
1175 /* ok , let us initialize the maxasc and maxdesc value.
1176 * This depends in LaTeX of the font of the last character
1177 * in the paragraph. The hack below is necessary because
1178 * of the possibility of open footnotes */
1180 /* Correction: only the fontsize count. The other properties
1181 are taken from the layoutfont. Nicer on the screen :) */
1182 Paragraph * par = row_ptr->par();
1183 Paragraph * firstpar = row_ptr->par();
1185 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1186 firstpar->getLayout());
1188 LyXFont font = getFont(bview->buffer(), par, par->size() - 1);
1189 LyXFont::FONT_SIZE const size = font.size();
1190 font = getFont(bview->buffer(), par, -1);
1193 LyXFont labelfont = getFont(bview->buffer(), par, -2);
1195 float spacing_val = 1.0;
1196 if (!row_ptr->par()->params().spacing().isDefault()) {
1197 spacing_val = row_ptr->par()->params().spacing().getValue();
1199 spacing_val = bview->buffer()->params.spacing.getValue();
1201 //lyxerr << "spacing_val = " << spacing_val << endl;
1203 int maxasc = int(lyxfont::maxAscent(font) *
1204 layout.spacing.getValue() *
1206 int maxdesc = int(lyxfont::maxDescent(font) *
1207 layout.spacing.getValue() *
1209 int const pos_end = rowLast(row_ptr);
1213 // Check if any insets are larger
1214 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1215 if (row_ptr->par()->getChar(pos) == Paragraph::META_INSET) {
1216 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1217 tmpinset = row_ptr->par()->getInset(pos);
1219 #if 1 // this is needed for deep update on initialitation
1220 tmpinset->update(bview, tmpfont);
1222 asc = tmpinset->ascent(bview, tmpfont);
1223 desc = tmpinset->descent(bview, tmpfont);
1224 maxwidth += tmpinset->width(bview, tmpfont);
1225 maxasc = max(maxasc, asc);
1226 maxdesc = max(maxdesc, desc);
1229 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1233 // Check if any custom fonts are larger (Asger)
1234 // This is not completely correct, but we can live with the small,
1235 // cosmetic error for now.
1236 LyXFont::FONT_SIZE const maxsize =
1237 row_ptr->par()->highestFontInRange(row_ptr->pos(),
1239 if (maxsize > font.size()) {
1240 font.setSize(maxsize);
1242 asc = lyxfont::maxAscent(font);
1243 desc = lyxfont::maxDescent(font);
1250 // This is nicer with box insets:
1254 row_ptr->ascent_of_text(maxasc);
1256 // is it a top line?
1257 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1259 // some parksips VERY EASY IMPLEMENTATION
1260 if (bview->buffer()->params.paragraph_separation ==
1261 BufferParams::PARSEP_SKIP) {
1262 if (layout.isParagraph()
1263 && firstpar->getDepth() == 0
1264 && firstpar->previous())
1265 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1266 else if (firstpar->previous()
1267 && textclasslist.Style(bview->buffer()->params.textclass,
1268 firstpar->previous()->getLayout()).isParagraph()
1269 && firstpar->previous()->getDepth() == 0)
1270 // is it right to use defskip here too? (AS)
1271 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1274 // the paper margins
1275 if (!row_ptr->par()->previous() && bv_owner)
1276 maxasc += LYX_PAPER_MARGIN;
1278 // add the vertical spaces, that the user added
1279 if (firstpar->params().spaceTop().kind() != VSpace::NONE)
1280 maxasc += int(firstpar->params().spaceTop().inPixels(bview));
1282 // do not forget the DTP-lines!
1283 // there height depends on the font of the nearest character
1284 if (firstpar->params().lineTop())
1285 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1288 // and now the pagebreaks
1289 if (firstpar->params().pagebreakTop())
1290 maxasc += 3 * defaultHeight();
1292 // This is special code for the chapter, since the label of this
1293 // layout is printed in an extra row
1294 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1295 && bview->buffer()->params.secnumdepth >= 0) {
1296 float spacing_val = 1.0;
1297 if (!row_ptr->par()->params().spacing().isDefault()) {
1298 spacing_val = row_ptr->par()->params().spacing().getValue();
1300 spacing_val = bview->buffer()->params.spacing.getValue();
1303 labeladdon = int(lyxfont::maxDescent(labelfont) *
1304 layout.spacing.getValue() *
1306 + int(lyxfont::maxAscent(labelfont) *
1307 layout.spacing.getValue() *
1311 // special code for the top label
1312 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1313 || layout.labeltype == LABEL_BIBLIO
1314 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1315 && row_ptr->par()->isFirstInSequence()
1316 && !row_ptr->par()->getLabelstring().empty()) {
1317 float spacing_val = 1.0;
1318 if (!row_ptr->par()->params().spacing().isDefault()) {
1319 spacing_val = row_ptr->par()->params().spacing().getValue();
1321 spacing_val = bview->buffer()->params.spacing.getValue();
1325 (lyxfont::maxAscent(labelfont) *
1326 layout.spacing.getValue() *
1328 +(lyxfont::maxDescent(labelfont) *
1329 layout.spacing.getValue() *
1331 + layout.topsep * defaultHeight()
1332 + layout.labelbottomsep * defaultHeight());
1335 // and now the layout spaces, for example before and after a section,
1336 // or between the items of a itemize or enumerate environment
1338 if (!firstpar->params().pagebreakTop()) {
1339 Paragraph * prev = row_ptr->par()->previous();
1341 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1342 if (prev && prev->getLayout() == firstpar->getLayout()
1343 && prev->getDepth() == firstpar->getDepth()
1344 && prev->getLabelWidthString() == firstpar->getLabelWidthString())
1346 layoutasc = (layout.itemsep * defaultHeight());
1348 else if (row_ptr->previous()) {
1349 tmptop = layout.topsep;
1351 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1352 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1353 row_ptr->previous()->par()->
1354 getLayout()).bottomsep;
1357 layoutasc = (tmptop * defaultHeight());
1359 else if (row_ptr->par()->params().lineTop()) {
1360 tmptop = layout.topsep;
1363 layoutasc = (tmptop * defaultHeight());
1366 prev = row_ptr->par()->outerHook();
1368 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1369 prev->getLayout()).parsep * defaultHeight());
1372 if (firstpar->previous()
1373 && firstpar->previous()->getDepth() == 0
1374 && firstpar->previous()->getLayout() != firstpar->getLayout()) {
1377 else if (firstpar->previous()){
1378 maxasc += int(layout.parsep * defaultHeight());
1384 // is it a bottom line?
1385 if (row_ptr->par() == par
1386 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
1388 // the paper margins
1389 if (!par->next() && bv_owner)
1390 maxdesc += LYX_PAPER_MARGIN;
1392 // add the vertical spaces, that the user added
1393 if (firstpar->params().spaceBottom().kind() != VSpace::NONE)
1394 maxdesc += int(firstpar->params().spaceBottom().inPixels(bview));
1396 // do not forget the DTP-lines!
1397 // there height depends on the font of the nearest character
1398 if (firstpar->params().lineBottom())
1399 maxdesc += 2 * lyxfont::ascent('x',
1400 getFont(bview->buffer(),
1401 par, par->size() - 1));
1403 // and now the pagebreaks
1404 if (firstpar->params().pagebreakBottom())
1405 maxdesc += 3 * defaultHeight();
1407 // and now the layout spaces, for example before and after
1408 // a section, or between the items of a itemize or enumerate
1410 if (!firstpar->params().pagebreakBottom() && row_ptr->par()->next()) {
1411 Paragraph * nextpar = row_ptr->par()->next();
1412 Paragraph * comparepar = row_ptr->par();
1416 if (comparepar->getDepth() > nextpar->getDepth()) {
1417 usual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1418 comparepar = comparepar->depthHook(nextpar->getDepth());
1419 if (comparepar->getLayout()!= nextpar->getLayout()
1420 || nextpar->getLabelWidthString() !=
1421 comparepar->getLabelWidthString())
1422 unusual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1424 if (unusual > usual)
1425 layoutdesc = unusual;
1429 else if (comparepar->getDepth() == nextpar->getDepth()) {
1431 if (comparepar->getLayout()!= nextpar->getLayout()
1432 || nextpar->getLabelWidthString() !=
1433 comparepar->getLabelWidthString())
1434 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1439 // incalculate the layout spaces
1440 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1441 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1443 // calculate the new height of the text
1444 height -= row_ptr->height();
1446 row_ptr->height(maxasc + maxdesc + labeladdon);
1447 row_ptr->baseline(maxasc + labeladdon);
1449 height += row_ptr->height();
1452 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1453 row_ptr->width(int(maxwidth + x));
1456 width = max(0,workWidth(bview));
1458 if (r->width() > width)
1466 /* Appends the implicit specified paragraph behind the specified row,
1467 * start at the implicit given position */
1468 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1470 bool not_ready = true;
1472 // The last character position of a paragraph is an invariant so we can
1473 // safely get it here. (Asger)
1474 int const lastposition = row->par()->size();
1476 // Get the next breakpoint
1477 int z = nextBreakPoint(bview, row, workWidth(bview));
1481 // Insert the new row
1482 if (z < lastposition) {
1484 insertRow(row, row->par(), z);
1491 // Set the dimensions of the row
1492 #ifdef WITH_WARNINGS
1493 #warning Something is rotten here! (Jug)
1495 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1496 setHeightOfRow(bview, tmprow);
1498 } while (not_ready);
1502 void LyXText::breakAgain(BufferView * bview, Row * row) const
1504 bool not_ready = true;
1507 // get the next breakpoint
1508 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1511 if (z < row->par()->size()) {
1512 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1515 insertRow(row, row->par(), z);
1521 if (row->pos() == z)
1522 not_ready = false; // the rest will not change
1528 /* if there are some rows too much, delete them */
1529 /* only if you broke the whole paragraph! */
1530 Row * tmprow2 = row;
1531 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1532 tmprow2 = tmprow2->next();
1534 while (tmprow2 != row) {
1535 tmprow2 = tmprow2->previous();
1536 removeRow(tmprow2->next());
1541 /* set the dimensions of the row */
1542 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1543 setHeightOfRow(bview, tmprow);
1544 } while (not_ready);
1548 // this is just a little changed version of break again
1549 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1551 // get the next breakpoint
1552 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1555 if (z < row->par()->size()) {
1557 || (row->next() && row->next()->par() != row->par())) {
1558 /* insert a new row */
1560 insertRow(row, row->par(), z);
1566 if (row->pos() != z)
1570 // if there are some rows too much, delete them
1571 // only if you broke the whole paragraph!
1572 Row * tmprow2 = row;
1573 while (tmprow2->next()
1574 && tmprow2->next()->par() == row->par()) {
1575 tmprow2 = tmprow2->next();
1577 while (tmprow2 != row) {
1578 tmprow2 = tmprow2->previous();
1579 removeRow(tmprow2->next());
1583 // set the dimensions of the row
1584 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1585 setHeightOfRow(bview, tmprow);
1589 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1591 LyXLayout const & layout =
1592 textclasslist.Style(bview->buffer()->params.textclass,
1593 cursor.par()->getLayout());
1595 // this is only allowed, if the current paragraph is not empty or caption
1596 if ((cursor.par()->size() <= 0)
1597 && layout.labeltype!= LABEL_SENSITIVE)
1600 setUndo(bview->buffer(), Undo::INSERT,
1601 cursor.par()->previous(),
1602 cursor.par()->next());
1604 // Always break behind a space
1606 // It is better to erase the space (Dekel)
1607 if (cursor.pos() < cursor.par()->size()
1608 && cursor.par()->isLineSeparator(cursor.pos()))
1609 cursor.par()->erase(cursor.pos());
1610 // cursor.pos(cursor.pos() + 1);
1612 // break the paragraph
1616 keep_layout = layout.isEnvironment();
1617 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1620 // well this is the caption hack since one caption is really enough
1621 if (layout.labeltype == LABEL_SENSITIVE) {
1623 // set to standard-layout
1624 cursor.par()->setLayout(0);
1626 // set to standard-layout
1627 cursor.par()->next()->setLayout(0);
1630 /* if the cursor is at the beginning of a row without prior newline,
1632 * This touches only the screen-update. Otherwise we would may have
1633 * an empty row on the screen */
1634 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1635 && cursor.row()->pos() == cursor.pos()) {
1639 status = LyXText::NEED_MORE_REFRESH;
1640 refresh_row = cursor.row();
1641 refresh_y = cursor.y() - cursor.row()->baseline();
1643 // Do not forget the special right address boxes
1644 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1645 while (refresh_row->previous() &&
1646 refresh_row->previous()->par() == refresh_row->par()) {
1647 refresh_row = refresh_row->previous();
1648 refresh_y -= refresh_row->height();
1651 removeParagraph(cursor.row());
1653 // set the dimensions of the cursor row
1654 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1656 setHeightOfRow(bview, cursor.row());
1658 while (cursor.par()->next()->size()
1659 && cursor.par()->next()->isNewline(0))
1660 cursor.par()->next()->erase(0);
1662 insertParagraph(bview, cursor.par()->next(), cursor.row());
1664 updateCounters(bview, cursor.row()->previous());
1666 /* This check is necessary. Otherwise the new empty paragraph will
1667 * be deleted automatically. And it is more friendly for the user! */
1669 setCursor(bview, cursor.par()->next(), 0);
1671 setCursor(bview, cursor.par(), 0);
1673 if (cursor.row()->next())
1674 breakAgain(bview, cursor.row()->next());
1680 // Just a macro to make some thing easier.
1681 void LyXText::redoParagraph(BufferView * bview) const
1683 clearSelection(bview);
1684 redoParagraphs(bview, cursor, cursor.par()->next());
1685 setCursorIntern(bview, cursor.par(), cursor.pos());
1689 /* insert a character, moves all the following breaks in the
1690 * same Paragraph one to the right and make a rebreak */
1691 void LyXText::insertChar(BufferView * bview, char c)
1693 setUndo(bview->buffer(), Undo::INSERT,
1694 cursor.par()->previous(),
1695 cursor.par()->next());
1697 // When the free-spacing option is set for the current layout,
1698 // disable the double-space checking
1700 bool const freeSpacing =
1701 textclasslist.Style(bview->buffer()->params.textclass,
1702 cursor.row()->par()->getLayout()).free_spacing;
1705 if (lyxrc.auto_number) {
1706 static string const number_operators = "+-/*";
1707 static string const number_unary_operators = "+-";
1708 static string const number_seperators = ".,:";
1710 if (current_font.number() == LyXFont::ON) {
1711 if (!isdigit(c) && !contains(number_operators, c) &&
1712 !(contains(number_seperators, c) &&
1713 cursor.pos() >= 1 &&
1714 cursor.pos() < cursor.par()->size() &&
1715 getFont(bview->buffer(),
1717 cursor.pos()).number() == LyXFont::ON &&
1718 getFont(bview->buffer(),
1720 cursor.pos()-1).number() == LyXFont::ON)
1722 Number(bview); // Set current_font.number to OFF
1723 } else if (isdigit(c) &&
1724 real_current_font.isVisibleRightToLeft()) {
1725 Number(bview); // Set current_font.number to ON
1727 if (cursor.pos() > 0) {
1728 char const c = cursor.par()->getChar(cursor.pos() - 1);
1729 if (contains(number_unary_operators, c) &&
1730 (cursor.pos() == 1 ||
1731 cursor.par()->isSeparator(cursor.pos() - 2) ||
1732 cursor.par()->isNewline(cursor.pos() - 2) )
1734 setCharFont(bview->buffer(),
1738 } else if (contains(number_seperators, c) &&
1739 cursor.pos() >= 2 &&
1740 getFont(bview->buffer(),
1742 cursor.pos()-2).number() == LyXFont::ON) {
1743 setCharFont(bview->buffer(),
1753 /* First check, if there will be two blanks together or a blank at
1754 the beginning of a paragraph.
1755 I decided to handle blanks like normal characters, the main
1756 difference are the special checks when calculating the row.fill
1757 (blank does not count at the end of a row) and the check here */
1759 // The bug is triggered when we type in a description environment:
1760 // The current_font is not changed when we go from label to main text
1761 // and it should (along with realtmpfont) when we type the space.
1762 // CHECK There is a bug here! (Asger)
1764 LyXFont realtmpfont = real_current_font;
1765 LyXFont rawtmpfont = current_font; /* store the current font.
1766 * This is because of the use
1767 * of cursor movements. The moving
1768 * cursor would refresh the
1771 // Get the font that is used to calculate the baselineskip
1772 Paragraph::size_type const lastpos = cursor.par()->size();
1773 LyXFont rawparfont =
1774 cursor.par()->getFontSettings(bview->buffer()->params,
1777 bool jumped_over_space = false;
1779 if (!freeSpacing && IsLineSeparatorChar(c)) {
1780 if ((cursor.pos() > 0
1781 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1782 || (cursor.pos() > 0
1783 && cursor.par()->isNewline(cursor.pos() - 1))
1784 || (cursor.pos() == 0)) {
1785 static bool sent_space_message = false;
1786 if (!sent_space_message) {
1787 if (cursor.pos() == 0)
1788 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1790 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1791 sent_space_message = true;
1796 } else if (IsNewlineChar(c)) {
1797 if (cursor.par() == cursor.par()
1798 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1802 /* No newline at first position
1803 * of a paragraph or behind labels.
1804 * TeX does not allow that. */
1806 if (cursor.pos() < cursor.par()->size() &&
1807 cursor.par()->isLineSeparator(cursor.pos()))
1808 // newline always after a blank!
1810 cursor.row()->fill(-1); // to force a new break
1813 // the display inset stuff
1814 if (cursor.row()->par()->getChar(cursor.row()->pos()) == Paragraph::META_INSET
1815 && cursor.row()->par()->getInset(cursor.row()->pos())
1816 && (cursor.row()->par()->getInset(cursor.row()->pos())->display() ||
1817 cursor.row()->par()->getInset(cursor.row()->pos())->needFullRow()))
1818 cursor.row()->fill(-1); // to force a new break
1820 // get the cursor row fist
1821 Row * row = cursor.row();
1822 int y = cursor.y() - row->baseline();
1823 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1824 * already insertet the character */
1825 cursor.par()->insertChar(cursor.pos(), c);
1826 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1828 if (!jumped_over_space) {
1829 // refresh the positions
1831 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1832 tmprow = tmprow->next();
1833 tmprow->pos(tmprow->pos() + 1);
1837 // Is there a break one row above
1838 if ((cursor.par()->isLineSeparator(cursor.pos())
1839 || cursor.par()->isNewline(cursor.pos())
1840 || cursor.row()->fill() == -1)
1841 && row->previous() && row->previous()->par() == row->par()) {
1842 Paragraph::size_type z = nextBreakPoint(bview,
1845 if (z >= row->pos()) {
1848 // set the dimensions of the row above
1849 row->previous()->fill(fill(bview,
1853 setHeightOfRow(bview, row->previous());
1855 y -= row->previous()->height();
1857 refresh_row = row->previous();
1858 status = LyXText::NEED_MORE_REFRESH;
1860 breakAgainOneRow(bview, row);
1862 current_font = rawtmpfont;
1863 real_current_font = realtmpfont;
1864 setCursor(bview, cursor.par(), cursor.pos() + 1,
1865 false, cursor.boundary());
1866 // cursor MUST be in row now.
1868 if (row->next() && row->next()->par() == row->par())
1869 need_break_row = row->next();
1873 // check, wether the last characters font has changed.
1874 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1875 && rawparfont != rawtmpfont)
1876 redoHeightOfParagraph(bview, cursor);
1883 // recalculate the fill of the row
1884 if (row->fill() >= 0) /* needed because a newline
1885 * will set fill to -1. Otherwise
1886 * we would not get a rebreak! */
1887 row->fill(fill(bview, row, workWidth(bview)));
1888 if (row->fill() < 0) {
1891 refresh_x = cursor.x();
1892 refresh_pos = cursor.pos();
1893 status = LyXText::NEED_MORE_REFRESH;
1894 breakAgainOneRow(bview, row);
1895 // will the cursor be in another row now?
1896 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1897 if (row->next() && row->next()->par() == row->par())
1898 // this should always be true
1900 breakAgainOneRow(bview, row);
1902 current_font = rawtmpfont;
1903 real_current_font = realtmpfont;
1905 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1907 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1908 != cursor.boundary())
1909 setCursor(bview, cursor.par(), cursor.pos(), false,
1910 !cursor.boundary());
1911 if (row->next() && row->next()->par() == row->par())
1912 need_break_row = row->next();
1917 refresh_x = cursor.x();
1919 refresh_pos = cursor.pos();
1921 int const tmpheight = row->height();
1922 setHeightOfRow(bview, row);
1923 if (tmpheight == row->height())
1924 status = LyXText::NEED_VERY_LITTLE_REFRESH;
1926 status = LyXText::NEED_MORE_REFRESH;
1928 current_font = rawtmpfont;
1929 real_current_font = realtmpfont;
1930 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1934 // check, wether the last characters font has changed.
1935 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1936 && rawparfont != rawtmpfont) {
1937 redoHeightOfParagraph(bview, cursor);
1939 // now the special right address boxes
1940 if (textclasslist.Style(bview->buffer()->params.textclass,
1941 cursor.par()->getLayout()).margintype
1942 == MARGIN_RIGHT_ADDRESS_BOX) {
1943 redoDrawingOfParagraph(bview, cursor);
1951 void LyXText::charInserted()
1953 // Here we could call FinishUndo for every 20 characters inserted.
1954 // This is from my experience how emacs does it.
1955 static unsigned int counter;
1965 void LyXText::prepareToPrint(BufferView * bview,
1966 Row * row, float & x,
1967 float & fill_separator,
1969 float & fill_label_hfill,
1975 float w = row->fill();
1977 fill_label_hfill = 0;
1979 fill_label_hfill = 0;
1982 row->par()->isRightToLeftPar(bview->buffer()->params);
1984 x = (workWidth(bview) > 0)
1985 ? rightMargin(bview->buffer(), row) : 0;
1987 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
1989 // is there a manual margin with a manual label
1990 if (textclasslist.Style(bview->buffer()->params.textclass,
1991 row->par()->getLayout()).margintype == MARGIN_MANUAL
1992 && textclasslist.Style(bview->buffer()->params.textclass,
1993 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
1995 /* one more since labels are left aligned */
1996 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
1997 if (nlh && !row->par()->getLabelWidthString().empty()) {
1998 fill_label_hfill = labelFill(bview, row) / nlh;
2002 // are there any hfills in the row?
2003 float const nh = numberOfHfills(bview->buffer(), row);
2006 fill_hfill = w / nh;
2008 // is it block, flushleft or flushright?
2009 // set x how you need it
2011 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
2012 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2014 align = row->par()->params().align();
2016 // center displayed insets
2018 if (row->par()->getChar(row->pos()) == Paragraph::META_INSET
2019 && (inset=row->par()->getInset(row->pos()))
2020 && (inset->display())) // || (inset->scroll() < 0)))
2021 align = (inset->LyxCode() == Inset::MATHMACRO_CODE)
2022 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2025 case LYX_ALIGN_BLOCK:
2026 ns = numberOfSeparators(bview->buffer(), row);
2027 if (ns && row->next() && row->next()->par() == row->par() &&
2028 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2029 && !(row->next()->par()->getChar(row->next()->pos()) == Paragraph::META_INSET
2030 && row->next()->par()->getInset(row->next()->pos())
2031 && row->next()->par()->getInset(row->next()->pos())->display())
2033 fill_separator = w / ns;
2037 case LYX_ALIGN_RIGHT:
2040 case LYX_ALIGN_CENTER:
2048 computeBidiTables(bview->buffer(), row);
2050 Paragraph::size_type main_body =
2051 beginningOfMainBody(bview->buffer(), row->par());
2052 Paragraph::size_type last = rowLast(row);
2054 if (main_body > 0 &&
2055 (main_body-1 > last ||
2056 !row->par()->isLineSeparator(main_body-1))) {
2057 LyXLayout const & layout =
2058 textclasslist.Style(bview->buffer()->params.textclass,
2059 row->par()->getLayout());
2060 x += lyxfont::width(layout.labelsep,
2061 getFont(bview->buffer(), row->par(), -2));
2062 if (main_body-1 <= last)
2063 x += fill_label_hfill;
2068 /* important for the screen */
2071 /* the cursor set functions have a special mechanism. When they
2072 * realize, that you left an empty paragraph, they will delete it.
2073 * They also delete the corresponding row */
2075 void LyXText::cursorRightOneWord(BufferView * bview) const
2077 // treat floats, HFills and Insets as words
2078 LyXCursor tmpcursor = cursor;
2079 // CHECK See comment on top of text.C
2081 if (tmpcursor.pos() == tmpcursor.par()->size()
2082 && tmpcursor.par()->next()) {
2083 tmpcursor.par(tmpcursor.par()->next());
2088 // Skip through initial nonword stuff.
2089 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2090 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2091 // printf("Current pos1 %d", tmpcursor.pos()) ;
2092 tmpcursor.pos(tmpcursor.pos() + 1);
2095 // Advance through word.
2096 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2097 tmpcursor.par()->isWord( tmpcursor.pos())) {
2098 // printf("Current pos2 %d", tmpcursor.pos()) ;
2099 tmpcursor.pos(tmpcursor.pos() + 1);
2103 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2107 void LyXText::cursorTab(BufferView * bview) const
2109 LyXCursor tmpcursor = cursor;
2110 while (tmpcursor.pos() < tmpcursor.par()->size()
2111 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2112 tmpcursor.pos(tmpcursor.pos() + 1);
2114 if (tmpcursor.pos() == tmpcursor.par()->size()){
2115 if (tmpcursor.par()->next()) {
2116 tmpcursor.par(tmpcursor.par()->next());
2120 tmpcursor.pos(tmpcursor.pos() + 1);
2121 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2125 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2126 of prior word, not to end of next prior word. */
2128 void LyXText::cursorLeftOneWord(BufferView * bview) const
2130 LyXCursor tmpcursor = cursor;
2131 cursorLeftOneWord(tmpcursor);
2132 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2135 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2137 // treat HFills, floats and Insets as words
2140 && (cur.par()->isSeparator(cur.pos() - 1)
2141 || cur.par()->isKomma(cur.pos() - 1))
2142 && !(cur.par()->isHfill(cur.pos() - 1)
2143 || cur.par()->isInset(cur.pos() - 1)))
2144 cur.pos(cur.pos() - 1);
2147 && (cur.par()->isInset(cur.pos() - 1)
2148 || cur.par()->isHfill(cur.pos() - 1))) {
2149 cur.pos(cur.pos() - 1);
2150 } else if (!cur.pos()) {
2151 if (cur.par()->previous()){
2152 cur.par(cur.par()->previous());
2153 cur.pos(cur.par()->size());
2155 } else { // Here, cur != 0
2156 while (cur.pos() > 0 &&
2157 cur.par()->isWord(cur.pos()-1) )
2158 cur.pos(cur.pos() - 1);
2162 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
2165 void LyXText::getWord(LyXCursor & from, LyXCursor & to, word_location loc) const
2167 // first put the cursor where we wana start to select the word
2171 // Move cursor to the beginning, when not already there.
2172 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2173 && !from.par()->isKomma(from.pos() - 1))
2174 cursorLeftOneWord(from);
2177 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2183 while (to.pos() < to.par()->size()
2184 && !to.par()->isSeparator(to.pos())
2185 && !to.par()->isKomma(to.pos())
2186 && !to.par()->isHfill(to.pos()) )
2188 to.pos(to.pos() + 1);
2193 void LyXText::selectWord(BufferView * bview)
2197 getWord(from, to, WHOLE_WORD);
2199 setCursor(bview, from.par(), from.pos());
2200 selection.cursor = cursor;
2201 setCursor(bview, to.par(), to.pos() );
2202 setSelection(bview);
2205 /* -------> Select the word currently under the cursor when:
2206 1: no selection is currently set,
2207 2: the cursor is not at the borders of the word. */
2209 bool LyXText::selectWordWhenUnderCursor(BufferView * bview)
2211 if (!selection.set() &&
2212 cursor.pos() > 0 && cursor.pos() < cursor.par()->size()
2213 && !cursor.par()->isSeparator(cursor.pos())
2214 && !cursor.par()->isKomma(cursor.pos())
2215 && !cursor.par()->isSeparator(cursor.pos() -1)
2216 && !cursor.par()->isKomma(cursor.pos() -1)) {
2224 // This function is only used by the spellchecker for NextWord().
2225 // It doesn't handle LYX_ACCENTs and probably never will.
2226 string const LyXText::selectNextWord(BufferView * bview,
2227 float & value) const
2229 Paragraph * tmppar = cursor.par();
2231 // If this is not the very first word, skip rest of
2232 // current word because we are probably in the middle
2233 // of a word if there is text here.
2234 if (cursor.pos() || cursor.par()->previous()) {
2235 while (cursor.pos() < cursor.par()->size()
2236 && cursor.par()->isLetter(cursor.pos()))
2237 cursor.pos(cursor.pos() + 1);
2240 // Now, skip until we have real text (will jump paragraphs)
2241 while ((cursor.par()->size() > cursor.pos()
2242 && (!cursor.par()->isLetter(cursor.pos())
2244 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2245 .latex() == LyXFont::ON
2248 || (cursor.par()->size() == cursor.pos()
2249 && cursor.par()->next())){
2250 if (cursor.pos() == cursor.par()->size()) {
2251 cursor.par(cursor.par()->next());
2254 cursor.pos(cursor.pos() + 1);
2257 // Update the value if we changed paragraphs
2258 if (cursor.par() != tmppar){
2259 setCursor(bview, cursor.par(), cursor.pos());
2260 value = float(cursor.y())/float(height);
2263 // Start the selection from here
2264 selection.cursor = cursor;
2266 std::ostringstream latex;
2268 // and find the end of the word
2269 // (optional hyphens are part of a word)
2270 while (cursor.pos() < cursor.par()->size()
2271 && (cursor.par()->isLetter(cursor.pos()))
2272 || (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET
2273 && cursor.par()->getInset(cursor.pos()) != 0
2274 && cursor.par()->getInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2275 && latex.str() == "\\-"
2277 cursor.pos(cursor.pos() + 1);
2279 // Finally, we copy the word to a string and return it
2281 if (selection.cursor.pos() < cursor.pos()) {
2282 Paragraph::size_type i;
2283 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2284 if (cursor.par()->getChar(i) != Paragraph::META_INSET)
2285 str += cursor.par()->getChar(i);
2292 // This one is also only for the spellchecker
2293 void LyXText::selectSelectedWord(BufferView * bview)
2295 // move cursor to the beginning
2296 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2298 // set the sel cursor
2299 selection.cursor = cursor;
2300 std::ostringstream latex;
2302 // now find the end of the word
2303 while (cursor.pos() < cursor.par()->size()
2304 && (cursor.par()->isLetter(cursor.pos())
2305 || (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET
2306 && cursor.par()->getInset(cursor.pos()) != 0
2307 && cursor.par()->getInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2308 && latex.str() == "\\-"
2310 cursor.pos(cursor.pos() + 1);
2312 setCursor(bview, cursor.par(), cursor.pos());
2314 // finally set the selection
2315 setSelection(bview);
2319 /* -------> Delete from cursor up to the end of the current or next word. */
2320 void LyXText::deleteWordForward(BufferView * bview)
2322 if (!cursor.par()->size())
2325 LyXCursor tmpcursor = cursor;
2326 tmpcursor.row(0); //Â ??
2327 selection.set(true); // to avoid deletion
2328 cursorRightOneWord(bview);
2329 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2330 selection.cursor = cursor;
2332 setSelection(bview);
2334 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2335 cutSelection(bview);
2340 /* -------> Delete from cursor to start of current or prior word. */
2341 void LyXText::deleteWordBackward(BufferView * bview)
2343 if (!cursor.par()->size())
2346 LyXCursor tmpcursor = cursor;
2347 tmpcursor.row(0); // ??
2348 selection.set(true); // to avoid deletion
2349 cursorLeftOneWord(bview);
2350 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2351 selection.cursor = cursor;
2353 setSelection(bview);
2354 cutSelection(bview);
2359 /* -------> Kill to end of line. */
2360 void LyXText::deleteLineForward(BufferView * bview)
2362 if (!cursor.par()->size())
2363 // Paragraph is empty, so we just go to the right
2366 LyXCursor tmpcursor = cursor;
2367 // We can't store the row over a regular setCursor
2368 // so we set it to 0 and reset it afterwards.
2369 tmpcursor.row(0); //Â ??
2370 selection.set(true); // to avoid deletion
2372 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2373 selection.cursor = cursor;
2375 setSelection(bview);
2376 // What is this test for ??? (JMarc)
2377 if (!selection.set()) {
2378 deleteWordForward(bview);
2380 cutSelection(bview);
2386 // Change the case of a word at cursor position.
2387 // This function directly manipulates Paragraph::text because there
2388 // is no Paragraph::SetChar currently. I did what I could to ensure
2389 // that it is correct. I guess part of it should be moved to
2390 // Paragraph, but it will have to change for 1.1 anyway. At least
2391 // it does not access outside of the allocated array as the older
2392 // version did. (JMarc)
2393 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2398 if (selection.set()) {
2399 from = selection.cursor;
2402 getWord(from, to, PARTIAL_WORD);
2403 setCursor(bview, to.par(), to.pos()+1);
2406 setUndo(bview->buffer(), Undo::FINISH,
2407 from.par()->previous(), to.par()->next());
2410 unsigned char c = from.par()->getChar(from.pos());
2411 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2413 case text_lowercase:
2416 case text_capitalization:
2418 action = text_lowercase;
2420 case text_uppercase:
2425 from.par()->setChar(from.pos(), c);
2426 checkParagraph(bview, from.par(), from.pos());
2427 from.pos(from.pos() + 1);
2428 if (from.pos() >= from.par()->size()) {
2429 from.par(from.par()->next());
2433 if (to.row() != from.row()) {
2434 refresh_y = from.y() - from.row()->baseline();
2435 refresh_row = from.row();
2436 status = LyXText::NEED_MORE_REFRESH;
2441 void LyXText::transposeChars(BufferView const & bview)
2443 Paragraph * tmppar = cursor.par();
2445 setUndo(bview.buffer(), Undo::FINISH,
2446 tmppar->previous(), tmppar->next());
2448 Paragraph::size_type tmppos = cursor.pos();
2450 // First decide if it is possible to transpose at all
2452 // We are at the beginning of a paragraph.
2453 if (tmppos == 0) return;
2455 // We are at the end of a paragraph.
2456 if (tmppos == tmppar->size() - 1) return;
2458 unsigned char c1 = tmppar->getChar(tmppos);
2459 unsigned char c2 = tmppar->getChar(tmppos - 1);
2461 if (c1 != Paragraph::META_INSET
2462 && c2 != Paragraph::META_INSET) {
2463 tmppar->setChar(tmppos, c2);
2464 tmppar->setChar(tmppos - 1, c1);
2466 // We should have an implementation that handles insets
2467 // as well, but that will have to come later. (Lgb)
2468 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2472 void LyXText::Delete(BufferView * bview)
2474 // this is a very easy implementation
2476 LyXCursor old_cursor = cursor;
2477 int const old_cur_par_id = old_cursor.par()->id();
2478 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2479 old_cursor.par()->previous()->id() : 0;
2481 // just move to the right
2484 // CHECK Look at the comment here.
2485 // This check is not very good...
2486 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2487 // and that can very well delete the par or par->previous in
2488 // old_cursor. Will a solution where we compare paragraph id's
2490 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2491 == old_cur_par_prev_id
2492 && cursor.par()->id() != old_cur_par_id)
2493 return; // delete-empty-paragraph-mechanism has done it
2495 // if you had success make a backspace
2496 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2497 LyXCursor tmpcursor = cursor;
2498 cursor = old_cursor; // to make sure undo gets the right cursor position
2499 setUndo(bview->buffer(), Undo::DELETE,
2500 cursor.par()->previous(),
2501 cursor.par()->next());
2508 void LyXText::backspace(BufferView * bview)
2510 // Get the font that is used to calculate the baselineskip
2511 Paragraph::size_type lastpos = cursor.par()->size();
2512 LyXFont rawparfont =
2513 cursor.par()->getFontSettings(bview->buffer()->params,
2516 if (cursor.pos() == 0) {
2517 // The cursor is at the beginning of a paragraph,
2518 // so the the backspace will collapse two paragraphs into one.
2520 // we may paste some paragraphs
2522 // is it an empty paragraph?
2525 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2526 // This is an empty paragraph and we delete it just by moving the cursor one step
2527 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2528 // of the paragraph.
2530 if (cursor.par()->previous()) {
2531 Paragraph * tmppar = cursor.par()->previous();
2532 if (cursor.par()->getLayout() == tmppar->getLayout()
2533 && cursor.par()->getAlign() == tmppar->getAlign()) {
2534 // Inherit bottom DTD from the paragraph below.
2535 // (the one we are deleting)
2536 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2537 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2538 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2543 // the layout things can change the height of a row !
2544 int const tmpheight = cursor.row()->height();
2545 setHeightOfRow(bview, cursor.row());
2546 if (cursor.row()->height() != tmpheight) {
2547 refresh_y = cursor.y() - cursor.row()->baseline();
2548 refresh_row = cursor.row();
2549 status = LyXText::NEED_MORE_REFRESH;
2555 if (cursor.par()->previous()) {
2556 setUndo(bview->buffer(), Undo::DELETE,
2557 cursor.par()->previous()->previous(),
2558 cursor.par()->next());
2561 Paragraph * tmppar = cursor.par();
2562 Row * tmprow = cursor.row();
2564 // We used to do cursorLeftIntern() here, but it is
2565 // not a good idea since it triggers the auto-delete
2566 // mechanism. So we do a cursorLeftIntern()-lite,
2567 // without the dreaded mechanism. (JMarc)
2568 if (cursor.par()->previous()) {
2569 // steps into the above paragraph.
2570 setCursorIntern(bview, cursor.par()->previous(),
2571 cursor.par()->previous()->size(),
2575 /* Pasting is not allowed, if the paragraphs have different
2576 layout. I think it is a real bug of all other
2577 word processors to allow it. It confuses the user.
2578 Even so with a footnote paragraph and a non-footnote
2579 paragraph. I will not allow pasting in this case,
2580 because the user would be confused if the footnote behaves
2581 different wether it is open or closed.
2583 Correction: Pasting is always allowed with standard-layout
2585 if (cursor.par() != tmppar
2586 && (cursor.par()->getLayout() == tmppar->getLayout()
2587 || tmppar->getLayout() == 0 /*standard*/)
2588 && cursor.par()->getAlign() == tmppar->getAlign()) {
2590 removeParagraph(tmprow);
2592 cursor.par()->pasteParagraph(bview->buffer()->params);
2594 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2595 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2596 // strangely enough it seems that commenting out the line above removes
2597 // most or all of the segfaults. I will however also try to move the
2598 // two Remove... lines in front of the PasteParagraph too.
2601 cursor.pos(cursor.pos() - 1);
2603 status = LyXText::NEED_MORE_REFRESH;
2604 refresh_row = cursor.row();
2605 refresh_y = cursor.y() - cursor.row()->baseline();
2607 // remove the lost paragraph
2608 // This one is not safe, since the paragraph that the tmprow and the
2609 // following rows belong to has been deleted by the PasteParagraph
2610 // above. The question is... could this be moved in front of the
2612 //RemoveParagraph(tmprow);
2613 //RemoveRow(tmprow);
2615 // This rebuilds the rows.
2616 appendParagraph(bview, cursor.row());
2617 updateCounters(bview, cursor.row());
2619 // the row may have changed, block, hfills etc.
2620 setCursor(bview, cursor.par(), cursor.pos(), false);
2623 /* this is the code for a normal backspace, not pasting
2625 setUndo(bview->buffer(), Undo::DELETE,
2626 cursor.par()->previous(),
2627 cursor.par()->next());
2628 // We used to do cursorLeftIntern() here, but it is
2629 // not a good idea since it triggers the auto-delete
2630 // mechanism. So we do a cursorLeftIntern()-lite,
2631 // without the dreaded mechanism. (JMarc)
2632 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2633 false, cursor.boundary());
2635 // some insets are undeletable here
2636 if (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
2637 if (!cursor.par()->getInset(cursor.pos())->Deletable())
2639 // force complete redo when erasing display insets
2640 // this is a cruel method but safe..... Matthias
2641 if (cursor.par()->getInset(cursor.pos())->display() ||
2642 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2643 cursor.par()->erase(cursor.pos());
2644 redoParagraph(bview);
2649 Row * row = cursor.row();
2650 int y = cursor.y() - row->baseline();
2651 Paragraph::size_type z;
2652 /* remember that a space at the end of a row doesnt count
2653 * when calculating the fill */
2654 if (cursor.pos() < rowLast(row) ||
2655 !cursor.par()->isLineSeparator(cursor.pos())) {
2656 row->fill(row->fill() + singleWidth(bview,
2661 /* some special code when deleting a newline. This is similar
2662 * to the behavior when pasting paragraphs */
2663 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2664 cursor.par()->erase(cursor.pos());
2665 // refresh the positions
2667 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2668 tmprow = tmprow->next();
2669 tmprow->pos(tmprow->pos() - 1);
2671 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2672 cursor.pos(cursor.pos() - 1);
2674 if (cursor.pos() < cursor.par()->size()
2675 && !cursor.par()->isSeparator(cursor.pos())) {
2676 cursor.par()->insertChar(cursor.pos(), ' ');
2677 setCharFont(bview->buffer(), cursor.par(),
2678 cursor.pos(), current_font);
2679 // refresh the positions
2681 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2682 tmprow = tmprow->next();
2683 tmprow->pos(tmprow->pos() + 1);
2687 cursor.par()->erase(cursor.pos());
2689 // refresh the positions
2691 while (tmprow->next()
2692 && tmprow->next()->par() == row->par()) {
2693 tmprow = tmprow->next();
2694 tmprow->pos(tmprow->pos() - 1);
2697 // delete newlines at the beginning of paragraphs
2698 while (cursor.par()->size() &&
2699 cursor.par()->isNewline(cursor.pos()) &&
2700 cursor.pos() == beginningOfMainBody(bview->buffer(),
2702 cursor.par()->erase(cursor.pos());
2703 // refresh the positions
2705 while (tmprow->next() &&
2706 tmprow->next()->par() == row->par()) {
2707 tmprow = tmprow->next();
2708 tmprow->pos(tmprow->pos() - 1);
2713 // is there a break one row above
2714 if (row->previous() && row->previous()->par() == row->par()) {
2715 z = nextBreakPoint(bview, row->previous(),
2717 if (z >= row->pos()) {
2720 Row * tmprow = row->previous();
2722 // maybe the current row is now empty
2723 if (row->pos() >= row->par()->size()) {
2728 breakAgainOneRow(bview, row);
2729 if (row->next() && row->next()->par() == row->par())
2730 need_break_row = row->next();
2735 // set the dimensions of the row above
2736 y -= tmprow->height();
2737 tmprow->fill(fill(bview, tmprow,
2739 setHeightOfRow(bview, tmprow);
2742 refresh_row = tmprow;
2743 status = LyXText::NEED_MORE_REFRESH;
2744 setCursor(bview, cursor.par(), cursor.pos(),
2745 false, cursor.boundary());
2746 //current_font = rawtmpfont;
2747 //real_current_font = realtmpfont;
2748 // check, whether the last character's font has changed.
2750 cursor.par()->getFontSettings(bview->buffer()->params,
2751 cursor.par()->size() - 1))
2752 redoHeightOfParagraph(bview, cursor);
2757 // break the cursor row again
2758 if (row->next() && row->next()->par() == row->par() &&
2759 (rowLast(row) == row->par()->size() - 1 ||
2760 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2762 /* it can happen that a paragraph loses one row
2763 * without a real breakup. This is when a word
2764 * is to long to be broken. Well, I don t care this
2766 if (rowLast(row) == row->par()->size() - 1)
2767 removeRow(row->next());
2771 status = LyXText::NEED_MORE_REFRESH;
2773 breakAgainOneRow(bview, row);
2774 // will the cursor be in another row now?
2775 if (row->next() && row->next()->par() == row->par() &&
2776 rowLast(row) <= cursor.pos()) {
2778 breakAgainOneRow(bview, row);
2781 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2783 if (row->next() && row->next()->par() == row->par())
2784 need_break_row = row->next();
2788 // set the dimensions of the row
2789 row->fill(fill(bview, row, workWidth(bview)));
2790 int const tmpheight = row->height();
2791 setHeightOfRow(bview, row);
2792 if (tmpheight == row->height())
2793 status = LyXText::NEED_VERY_LITTLE_REFRESH;
2795 status = LyXText::NEED_MORE_REFRESH;
2798 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2802 // current_font = rawtmpfont;
2803 // real_current_font = realtmpfont;
2805 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2806 != cursor.boundary())
2807 setCursor(bview, cursor.par(), cursor.pos(), false,
2808 !cursor.boundary());
2810 lastpos = cursor.par()->size();
2811 if (cursor.pos() == lastpos)
2812 setCurrentFont(bview);
2814 // check, whether the last characters font has changed.
2816 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2817 redoHeightOfParagraph(bview, cursor);
2819 // now the special right address boxes
2820 if (textclasslist.Style(bview->buffer()->params.textclass,
2821 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2822 redoDrawingOfParagraph(bview, cursor);
2828 void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
2829 Row * row_ptr, int y, bool cleared)
2831 // returns a printed row
2832 Painter & pain = bview->painter();
2835 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
2837 Paragraph::size_type const last = rowLastPrintable(row_ptr);
2839 Paragraph::size_type vpos;
2840 Paragraph::size_type pos;
2844 LyXFont font(LyXFont::ALL_SANE);
2846 if (row_ptr->height() <= 0) {
2847 lyxerr << "LYX_ERROR: row.height: "
2848 << row_ptr->height() << endl;
2853 float fill_separator;
2855 float fill_label_hfill;
2856 prepareToPrint(bview, row_ptr, x, fill_separator,
2857 fill_hfill, fill_label_hfill);
2859 if (inset_owner && (x < 0))
2863 // clear the area where we want to paint/print
2864 int const ww = bview->workWidth();
2866 bool clear_area = true;
2869 if (!bview->screen()->forceClear() && last == row_ptr->pos()
2870 && row_ptr->par()->getChar(row_ptr->pos()) == Paragraph::META_INSET
2871 && (inset = row_ptr->par()->getInset(row_ptr->pos()))) {
2872 clear_area = inset->doClearArea();
2874 // we don't need to clear it's already done!!!
2877 } else if (clear_area) {
2878 #ifdef WITH_WARNINGS
2879 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
2880 #warning Should we not fix this in the Painter, please have a look Lars! (Jug)
2882 int const y = y_offset < 0 ? 0 : y_offset;
2883 int const h = y_offset < 0 ?
2884 row_ptr->height() + y_offset : row_ptr->height();
2885 int const w = inset_owner ?
2886 inset_owner->width(bview, font) - 2 : ww;
2887 int const x = x_offset;
2888 pain.fillRectangle(x, y, w, h);
2889 } else if (inset != 0) {
2890 int h = row_ptr->baseline() - inset->ascent(bview, font);
2892 int const w = (inset_owner ?
2893 inset_owner->width(bview, font) : ww);
2894 pain.fillRectangle(x_offset, y_offset, w, h);
2896 h += inset->ascent(bview, font) + inset->descent(bview, font);
2897 if ((row_ptr->height() - h) > 0) {
2898 int const w = (inset_owner ?
2899 inset_owner->width(bview, font) : ww);
2900 pain.fillRectangle(x_offset, y_offset + h,
2901 w, row_ptr->height() - h);
2903 if (!inset_owner && !inset->display() && !inset->needFullRow())
2905 int const w = inset->width(bview, font) + int(x);
2906 pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
2910 if (selection.set()) {
2911 int const w = (inset_owner ?
2912 inset_owner->width(bview, font) : ww);
2914 if (bidi_same_direction) {
2915 if (selection.start.row() == row_ptr &&
2916 selection.end.row() == row_ptr) {
2917 if (selection.start.x() < selection.end.x())
2918 pain.fillRectangle(x_offset + selection.start.x(),
2920 selection.end.x() - selection.start.x(),
2924 pain.fillRectangle(x_offset + selection.end.x(),
2926 selection.start.x() - selection.end.x(),
2929 } else if (selection.start.row() == row_ptr) {
2931 pain.fillRectangle(x_offset, y_offset,
2932 selection.start.x(),
2936 pain.fillRectangle(x_offset + selection.start.x(),
2938 w - selection.start.x(),
2941 } else if (selection.end.row() == row_ptr) {
2943 pain.fillRectangle(x_offset + selection.end.x(),
2945 w - selection.end.x(),
2949 pain.fillRectangle(x_offset, y_offset,
2953 } else if (y > selection.start.y()
2954 && y < selection.end.y()) {
2955 pain.fillRectangle(x_offset, y_offset, w,
2959 } else if (selection.start.row() != row_ptr &&
2960 selection.end.row() != row_ptr &&
2961 y > selection.start.y()
2962 && y < selection.end.y()) {
2963 pain.fillRectangle(x_offset, y_offset, w,
2966 } else if (selection.start.row() == row_ptr ||
2967 selection.end.row() == row_ptr) {
2969 if ((selection.start.row() != row_ptr && !is_rtl) ||
2970 (selection.end.row() != row_ptr && is_rtl))
2971 pain.fillRectangle(x_offset, y_offset,
2975 Paragraph::size_type main_body =
2976 beginningOfMainBody(bview->buffer(),
2979 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
2980 pos = vis2log(vpos);
2981 float const old_tmpx = tmpx;
2982 if (main_body > 0 && pos == main_body-1) {
2983 tmpx += fill_label_hfill +
2984 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
2985 row_ptr->par()->getLayout()).labelsep,
2986 getFont(bview->buffer(),row_ptr->par(), -2));
2987 if (row_ptr->par()->isLineSeparator(main_body-1))
2988 tmpx -= singleWidth(bview, row_ptr->par(), main_body-1);
2990 if (hfillExpansion(bview->buffer(), row_ptr, pos)) {
2991 tmpx += singleWidth(bview, row_ptr->par(), pos);
2992 if (pos >= main_body)
2995 tmpx += fill_label_hfill;
2997 else if (row_ptr->par()->isSeparator(pos)) {
2998 tmpx += singleWidth(bview, row_ptr->par(), pos);
2999 if (pos >= main_body)
3000 tmpx += fill_separator;
3002 tmpx += singleWidth(bview, row_ptr->par(), pos);
3004 if ((selection.start.row() != row_ptr ||
3005 selection.start.pos() <= pos) &&
3006 (selection.end.row() != row_ptr ||
3007 pos < selection.end.pos()) )
3008 // Here we do not use x_offset as x_offset was
3010 pain.fillRectangle(int(old_tmpx),
3012 int(tmpx - old_tmpx + 1),
3017 if ((selection.start.row() != row_ptr && is_rtl) ||
3018 (selection.end.row() != row_ptr && !is_rtl) )
3019 pain.fillRectangle(x_offset + int(tmpx),
3029 // Draw appendix lines
3030 Paragraph * firstpar = row_ptr->par();
3032 if (firstpar->params().appendix()) {
3033 pain.line(1, y_offset,
3034 1, y_offset + row_ptr->height(),
3035 LColor::appendixline);
3036 pain.line(ww - 2, y_offset,
3037 ww - 2, y_offset + row_ptr->height(),
3038 LColor::appendixline);
3042 Paragraph::depth_type const depth = firstpar->getDepth();
3044 Paragraph::depth_type next_depth = 0;
3045 Paragraph::depth_type prev_depth = 0;
3046 if (row_ptr->next())
3047 next_depth = row_ptr->next()->par()->getDepth();
3048 if (row_ptr->previous())
3049 prev_depth = row_ptr->previous()->par()->getDepth();
3051 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3052 int const line_x = (LYX_PAPER_MARGIN / 5) *
3053 i + box_x + x_offset;
3054 pain.line(line_x, y_offset, line_x,
3055 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3059 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3062 pain.fillRectangle(line_x,
3063 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3064 LYX_PAPER_MARGIN / 5, 2,
3070 LyXLayout const & layout =
3071 textclasslist.Style(bview->buffer()->params.textclass,
3072 row_ptr->par()->getLayout());
3075 int y_bottom = row_ptr->height();
3077 // is it a first row?
3078 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3080 // start of appendix?
3081 if (row_ptr->par()->params().startOfAppendix()) {
3082 pain.line(1, y_offset,
3084 LColor::appendixline);
3087 // think about the margins
3088 if (!row_ptr->previous() && bv_owner)
3089 y_top += LYX_PAPER_MARGIN;
3091 // draw a top pagebreak
3092 if (row_ptr->par()->params().pagebreakTop()) {
3094 pb_font.setColor(LColor::pagebreak).decSize();
3098 pain.line(0, y_offset + y_top + 2*defaultHeight(),
3100 y_offset + y_top + 2 * defaultHeight(),
3102 Painter::line_onoffdash)
3105 _("Page Break (top)"),
3108 LColor::background, false, w, a, d);
3109 pain.rectText((ww - w)/2,
3110 y_offset + y_top + 2 * defaultHeight() + d,
3111 _("Page Break (top)"),
3114 LColor::background);
3115 y_top += 3 * defaultHeight();
3118 if (row_ptr->par()->params().spaceTop().kind() == VSpace::VFILL) {
3120 pain.line(0, y_offset + 2 + y_top,
3121 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3124 pain.line(0, y_offset + y_top + 3 * defaultHeight(),
3126 y_offset + y_top + 3 * defaultHeight(),
3129 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3130 LYX_PAPER_MARGIN / 2,
3131 y_offset + y_top + 3 * defaultHeight(),
3134 y_top += 3 * defaultHeight();
3137 // think about user added space
3138 y_top += int(row_ptr->par()->params().spaceTop().inPixels(bview));
3140 // think about the parskip
3141 // some parskips VERY EASY IMPLEMENTATION
3142 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3143 if (layout.latextype == LATEX_PARAGRAPH
3144 && firstpar->getDepth() == 0
3145 && firstpar->previous())
3146 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3147 else if (firstpar->previous()
3148 && textclasslist.Style(bview->buffer()->params.textclass,
3149 firstpar->previous()->getLayout()).latextype == LATEX_PARAGRAPH
3150 && firstpar->previous()->getDepth() == 0)
3151 // is it right to use defskip here, too? (AS)
3152 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3155 if (row_ptr->par()->params().lineTop()) {
3157 y_top += lyxfont::ascent('x',
3158 getFont(bview->buffer(),
3159 row_ptr->par(), 0));
3160 int const w = (inset_owner ?
3161 inset_owner->width(bview, font) : ww);
3162 int const xp = static_cast<int>(inset_owner ? x : 0);
3163 pain.line(xp, y_offset + y_top,
3164 w, y_offset + y_top,
3166 Painter::line_solid,
3167 Painter::line_thick);
3169 y_top += lyxfont::ascent('x',getFont(bview->buffer(),
3170 row_ptr->par(), 0));
3173 // should we print a label?
3174 if (layout.labeltype >= LABEL_STATIC
3175 && (layout.labeltype != LABEL_STATIC
3176 || layout.latextype != LATEX_ENVIRONMENT
3177 || row_ptr->par()->isFirstInSequence())) {
3178 font = getFont(bview->buffer(), row_ptr->par(), -2);
3179 if (!row_ptr->par()->getLabelstring().empty()) {
3181 string const tmpstring =
3182 row_ptr->par()->getLabelstring();
3184 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3185 if (bview->buffer()->params.secnumdepth >= 0) {
3186 // this is special code for
3187 // the chapter layout. This is
3188 // printed in an extra row
3189 // and has a pagebreak at
3191 float spacing_val = 1.0;
3192 if (!row_ptr->par()->params().spacing().isDefault()) {
3193 spacing_val = row_ptr->par()->params().spacing().getValue();
3195 spacing_val = bview->buffer()->params.spacing.getValue();
3198 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3199 + int(layout.parsep) * defaultHeight();
3201 tmpx = ww - leftMargin(bview, row_ptr) -
3202 lyxfont::width(tmpstring, font);
3203 pain.text(int(tmpx),
3204 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3209 tmpx = ww - leftMargin(bview, row_ptr)
3210 + lyxfont::width(layout.labelsep, font);
3212 tmpx = x - lyxfont::width(layout.labelsep, font)
3213 - lyxfont::width(tmpstring, font);
3216 pain.text(int(tmpx),
3217 y_offset + row_ptr->baseline(),
3221 // the labels at the top of an environment.
3222 // More or less for bibliography
3223 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3224 layout.labeltype == LABEL_BIBLIO ||
3225 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3226 if (row_ptr->par()->isFirstInSequence()) {
3227 font = getFont(bview->buffer(),
3228 row_ptr->par(), -2);
3229 if (!row_ptr->par()->getLabelstring().empty()) {
3230 string const tmpstring =
3231 row_ptr->par()->getLabelstring();
3232 float spacing_val = 1.0;
3233 if (!row_ptr->par()->params().spacing().isDefault()) {
3234 spacing_val = row_ptr->par()->params().spacing().getValue();
3236 spacing_val = bview->buffer()->params.spacing.getValue();
3239 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3240 + (layout.labelbottomsep * defaultHeight()));
3243 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3244 tmpx = ( (is_rtl ? leftMargin(bview, row_ptr) : x)
3245 + ww - rightMargin(bview->buffer(), row_ptr) ) / 2;
3246 tmpx -= lyxfont::width(tmpstring, font) / 2;
3248 tmpx = ww - leftMargin(bview, row_ptr) -
3249 lyxfont::width(tmpstring, font);
3250 pain.text(int(tmpx),
3251 y_offset + row_ptr->baseline()
3252 - row_ptr->ascent_of_text()
3258 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3259 font = getFont(bview->buffer(), row_ptr->par(), -1);
3261 tmpx = ww - leftMargin(bview, row_ptr)
3262 + lyxfont::width(layout.labelsep, font);
3264 tmpx = x - lyxfont::width(layout.labelsep, font)
3265 - row_ptr->par()->bibkey->width(bview, font);
3266 row_ptr->par()->bibkey->draw(bview, font,
3267 y_offset + row_ptr->baseline(),
3272 // is it a last row?
3273 Paragraph * par = row_ptr->par();
3274 if (row_ptr->par() == par
3275 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
3276 // think about the margins
3277 if (!row_ptr->next() && bv_owner)
3278 y_bottom -= LYX_PAPER_MARGIN;
3280 // draw a bottom pagebreak
3281 if (firstpar->params().pagebreakBottom()) {
3283 pb_font.setColor(LColor::pagebreak).decSize();
3284 int const y_place = y_offset + y_bottom
3285 - 2 * defaultHeight();
3291 .line(0, y_place, ww, y_place,
3293 Painter::line_onoffdash)
3295 _("Page Break (bottom)"),
3298 LColor::background, false, w, a, d);
3299 pain.rectText((ww - w) / 2, y_place + d,
3300 _("Page Break (bottom)"),
3303 LColor::background);
3304 y_bottom -= 3 * defaultHeight();
3307 if (firstpar->params().spaceBottom().kind() == VSpace::VFILL) {
3308 // draw a vfill bottom
3309 int const y_place = y_offset + y_bottom
3310 - 3 * defaultHeight();
3312 pain.line(0, y_place,
3313 LYX_PAPER_MARGIN, y_place,
3315 pain.line(0, y_offset + y_bottom - 2,
3317 y_offset + y_bottom - 2,
3319 pain.line(LYX_PAPER_MARGIN / 2,
3321 LYX_PAPER_MARGIN / 2,
3322 y_offset + y_bottom - 2,
3324 y_bottom -= 3 * defaultHeight();
3327 // think about user added space
3328 y_bottom -= int(firstpar->params().spaceBottom().inPixels(bview));
3330 if (firstpar->params().lineBottom()) {
3331 // draw a bottom line
3332 y_bottom -= lyxfont::ascent('x',
3333 getFont(bview->buffer(),
3336 int const w = (inset_owner ?
3337 inset_owner->width(bview, font) : ww);
3338 int const xp = static_cast<int>(inset_owner ? x : 0);
3339 pain.line(xp, y_offset + y_bottom,
3340 w, y_offset + y_bottom,
3341 LColor::topline, Painter::line_solid,
3342 Painter::line_thick);
3343 y_bottom -= lyxfont::ascent('x',
3344 getFont(bview->buffer(),
3350 int const endlabel =
3351 row_ptr->par()->getEndLabel(bview->buffer()->params);
3354 case END_LABEL_FILLED_BOX:
3356 LyXFont const font = getFont(bview->buffer(),
3357 row_ptr->par(), last);
3358 int const size = int(0.75 * lyxfont::maxAscent(font));
3359 int const y = (y_offset + row_ptr->baseline()) - size;
3360 int x = is_rtl ? LYX_PAPER_MARGIN
3361 : ww - LYX_PAPER_MARGIN - size;
3363 if (row_ptr->fill() <= size)
3364 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3365 if (endlabel == END_LABEL_BOX) {
3366 pain.line(x, y, x, y + size,
3368 pain.line(x + size, y, x + size , y + size,
3370 pain.line(x, y, x + size, y,
3372 pain.line(x, y + size, x + size, y + size,
3375 pain.fillRectangle(x, y, size, size,
3379 case END_LABEL_STATIC:
3381 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->getLayout();
3382 string const tmpstring = textclasslist.
3383 Style(bview->buffer()->params.textclass,
3384 layout).endlabelstring();
3385 font = getFont(bview->buffer(), row_ptr->par(), -2);
3386 int const tmpx = is_rtl ?
3387 int(x) - lyxfont::width(tmpstring, font)
3388 : ww - rightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
3389 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
3392 case END_LABEL_NO_LABEL:
3397 // draw the text in the pixmap
3399 vpos = row_ptr->pos();
3401 Paragraph::size_type main_body =
3402 beginningOfMainBody(bview->buffer(), row_ptr->par());
3403 if (main_body > 0 &&
3404 (main_body-1 > last ||
3405 !row_ptr->par()->isLineSeparator(main_body - 1)))
3408 while (vpos <= last) {
3409 pos = vis2log(vpos);
3410 if (main_body > 0 && pos == main_body - 1) {
3411 x += fill_label_hfill
3412 + lyxfont::width(layout.labelsep,
3413 getFont(bview->buffer(),
3414 row_ptr->par(), -2))
3415 - singleWidth(bview,
3420 if (row_ptr->par() ->isHfill(pos)) {
3423 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3425 y_offset + row_ptr->baseline(),
3428 if (hfillExpansion(bview->buffer(),
3430 if (pos >= main_body) {
3432 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3433 int(x + fill_hfill),
3434 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3436 Painter::line_onoffdash);
3440 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3441 int(x + fill_label_hfill),
3442 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3444 Painter::line_onoffdash);
3446 x += fill_label_hfill;
3449 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3451 y_offset + row_ptr->baseline(),
3456 } else if (row_ptr->par()->isSeparator(pos)) {
3457 x += singleWidth(bview,
3458 row_ptr->par(), pos);
3459 if (pos >= main_body)
3460 x += fill_separator;
3463 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
3468 int LyXText::defaultHeight() const
3470 LyXFont font(LyXFont::ALL_SANE);
3471 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3475 /* returns the column near the specified x-coordinate of the row
3476 * x is set to the real beginning of this column */
3477 int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3478 bool & boundary) const
3481 float fill_separator;
3483 float fill_label_hfill;
3485 prepareToPrint(bview, row, tmpx, fill_separator,
3486 fill_hfill, fill_label_hfill);
3488 Paragraph::size_type vc = row->pos();
3489 Paragraph::size_type last = rowLastPrintable(row);
3490 Paragraph::size_type c = 0;
3491 LyXLayout const & layout =
3492 textclasslist.Style(bview->buffer()->params.textclass,
3493 row->par()->getLayout());
3494 bool left_side = false;
3496 Paragraph::size_type
3497 main_body = beginningOfMainBody(bview->buffer(), row->par());
3498 float last_tmpx = tmpx;
3500 if (main_body > 0 &&
3501 (main_body - 1 > last ||
3502 !row->par()->isLineSeparator(main_body - 1)))
3505 while (vc <= last && tmpx <= x) {
3508 if (main_body > 0 && c == main_body-1) {
3509 tmpx += fill_label_hfill +
3510 lyxfont::width(layout.labelsep,
3511 getFont(bview->buffer(), row->par(), -2));
3512 if (row->par()->isLineSeparator(main_body - 1))
3513 tmpx -= singleWidth(bview, row->par(), main_body-1);
3516 if (hfillExpansion(bview->buffer(), row, c)) {
3517 x += singleWidth(bview, row->par(), c);
3521 tmpx += fill_label_hfill;
3523 else if (row->par()->isSeparator(c)) {
3524 tmpx += singleWidth(bview, row->par(), c);
3526 tmpx+= fill_separator;
3528 tmpx += singleWidth(bview, row->par(), c);
3532 if ((tmpx + last_tmpx) / 2 > x) {
3537 if (vc > last + 1) // This shouldn't happen.
3541 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3542 // some speedup if rtl_support=false
3543 && (!row->next() || row->next()->par() != row->par());
3544 bool const rtl = (lastrow)
3545 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3546 : false; // If lastrow is false, we don't need to compute
3547 // the value of rtl.
3549 if (row->pos() > last) // Row is empty?
3552 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3553 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3555 else if (vc == row->pos()) {
3557 if (bidi_level(c) % 2 == 1)
3560 c = vis2log(vc - 1);
3561 bool const rtl = (bidi_level(c) % 2 == 1);
3562 if (left_side == rtl) {
3564 boundary = isBoundary(bview->buffer(), row->par(), c);
3568 if (row->pos() <= last && c > last
3569 && row->par()->isNewline(last)) {
3570 if (bidi_level(last) % 2 == 0)
3571 tmpx -= singleWidth(bview, row->par(), last);
3573 tmpx += singleWidth(bview, row->par(), last);
3583 // returns pointer to a specified row
3584 Row * LyXText::getRow(Paragraph * par,
3585 Paragraph::size_type pos, int & y) const
3590 Row * tmprow = firstrow;
3593 // find the first row of the specified paragraph
3594 while (tmprow->next() && tmprow->par() != par) {
3595 y += tmprow->height();
3596 tmprow = tmprow->next();
3599 // now find the wanted row
3600 while (tmprow->pos() < pos
3602 && tmprow->next()->par() == par
3603 && tmprow->next()->pos() <= pos) {
3604 y += tmprow->height();
3605 tmprow = tmprow->next();