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"
38 #include "undo_funcs.h"
47 int const LYX_PAPER_MARGIN = 20;
51 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
54 int LyXText::workWidth(BufferView * bview) const
57 return inset_owner->textWidth(bview);
59 return bview->workWidth();
63 int LyXText::getRealCursorX(BufferView * bview) const
66 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
67 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
72 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
73 Paragraph::size_type pos) const
75 if (!Encodings::is_arabic(c))
76 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
77 return c + (0xb0 - '0');
81 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
82 unsigned char next_char = ' ';
84 for (Paragraph::size_type i = pos+1; i < par->size(); ++i)
85 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
86 next_char = par->getChar(i);
90 if (Encodings::is_arabic(next_char)) {
91 if (Encodings::is_arabic(prev_char))
92 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
94 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
96 if (Encodings::is_arabic(prev_char))
97 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
99 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
103 // This is the comments that some of the warnings below refers to.
104 // There are some issues in this file and I don't think they are
105 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
106 // this is a problem that has been here almost from day one and that a
107 // larger userbase with differenct access patters triggers the bad
108 // behaviour. (segfaults.) What I think happen is: In several places
109 // we store the paragraph in the current cursor and then moves the
110 // cursor. This movement of the cursor will delete paragraph at the
111 // old position if it is now empty. This will make the temporary
112 // pointer to the old cursor paragraph invalid and dangerous to use.
113 // And is some cases this will trigger a segfault. I have marked some
114 // of the cases where this happens with a warning, but I am sure there
115 // are others in this file and in text2.C. There is also a note in
116 // Delete() that you should read. In Delete I store the paragraph->id
117 // instead of a pointer to the paragraph. I am pretty sure this faulty
118 // use of temporary pointers to paragraphs that might have gotten
119 // invalidated (through a cursor movement) before they are used, are
120 // the cause of the strange crashes we get reported often.
122 // It is very tiresom to change this code, especially when it is as
123 // hard to read as it is. Help to fix all the cases where this is done
124 // would be greately appreciated.
128 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
129 Paragraph::size_type pos) const
131 char const c = par->getChar(pos);
132 return singleWidth(bview, par, pos, c);
136 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
137 Paragraph::size_type pos, char c) const
139 LyXFont const font = getFont(bview->buffer(), par, pos);
141 // The most common case is handled first (Asger)
142 if (IsPrintable(c)) {
143 if (font.language()->RightToLeft()) {
144 if (font.language()->lang() == "arabic" &&
145 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
146 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
147 if (Encodings::IsComposeChar_arabic(c))
150 c = transformChar(c, par, pos);
151 else if (font.language()->lang() == "hebrew" &&
152 Encodings::IsComposeChar_hebrew(c))
155 return lyxfont::width(c, font);
157 } else if (IsHfillChar(c)) {
158 return 3; /* Because of the representation
159 * as vertical lines */
160 } else if (c == Paragraph::META_INSET) {
161 Inset * tmpinset = par->getInset(pos);
163 #if 0 // seems not to be needed, but ...
164 tmpinset->update(bview, font);
166 return tmpinset->width(bview, font);
170 } else if (IsSeparatorChar(c))
172 else if (IsNewlineChar(c))
174 return lyxfont::width(c, font);
178 // Returns the paragraph position of the last character in the specified row
179 Paragraph::size_type LyXText::rowLast(Row const * row) const
181 if (row->next() == 0)
182 return row->par()->size() - 1;
183 else if (row->next()->par() != row->par())
184 return row->par()->size() - 1;
186 return row->next()->pos() - 1;
190 Paragraph::size_type LyXText::rowLastPrintable(Row const * row) const
192 Paragraph::size_type const last = rowLast(row);
193 if (last >= row->pos()
195 && row->next()->par() == row->par()
196 && row->par()->isSeparator(last))
203 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
205 bidi_same_direction = true;
206 if (!lyxrc.rtl_support) {
211 bidi_start = row->pos();
212 bidi_end = rowLastPrintable(row);
214 if (bidi_start > bidi_end) {
219 if (bidi_end + 2 - bidi_start >
220 static_cast<Paragraph::size_type>(log2vis_list.size())) {
221 Paragraph::size_type new_size =
222 (bidi_end + 2 - bidi_start < 500) ?
223 500 : 2 * (bidi_end + 2 - bidi_start);
224 log2vis_list.resize(new_size);
225 vis2log_list.resize(new_size);
226 bidi_levels.resize(new_size);
229 vis2log_list[bidi_end + 1 - bidi_start] = -1;
230 log2vis_list[bidi_end + 1 - bidi_start] = -1;
232 Paragraph::size_type stack[2];
234 row->par()->getParLanguage(buf->params)->RightToLeft();
238 Paragraph::size_type const main_body =
239 beginningOfMainBody(buf, row->par());
241 for (Paragraph::size_type lpos = bidi_start;
242 lpos <= bidi_end; ++lpos) {
243 bool is_space = row->par()->isLineSeparator(lpos);
244 Paragraph::size_type const pos =
245 (is_space && lpos + 1 <= bidi_end &&
246 !row->par()->isLineSeparator(lpos + 1) &&
247 !row->par()->isNewline(lpos + 1))
249 LyXFont font = row->par()->getFontSettings(buf->params, pos);
250 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
251 font.number() == LyXFont::ON &&
252 row->par()->getFontSettings(buf->params, lpos-1).number()
254 font = row->par()->getFontSettings(buf->params, lpos);
259 bool new_rtl = font.isVisibleRightToLeft();
260 bool new_rtl0 = font.isRightToLeft();
263 if (lpos == main_body - 1
264 && row->pos() < main_body - 1
266 new_level = (rtl_par) ? 1 : 0;
267 new_rtl = new_rtl0 = rtl_par;
269 new_level = (new_rtl) ? 1 : 2;
271 new_level = (rtl_par) ? 2 : 0;
273 if (is_space && new_level >= level) {
279 int new_level2 = new_level;
281 if (level == new_level && rtl0 != new_rtl0) {
283 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
284 } else if (level < new_level) {
285 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
286 if (new_level > rtl_par)
287 bidi_same_direction = false;
289 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
292 bidi_levels[lpos - bidi_start] = new_level;
294 while (level > new_level2) {
295 Paragraph::size_type old_lpos =
297 int delta = lpos - old_lpos - 1;
300 log2vis_list[lpos - bidi_start] += delta;
301 log2vis_list[old_lpos - bidi_start] += delta;
303 while (level < new_level)
304 stack[level++] = lpos;
308 Paragraph::size_type const old_lpos = stack[--level];
309 int delta = bidi_end - old_lpos;
312 log2vis_list[old_lpos - bidi_start] += delta;
315 Paragraph::size_type vpos = bidi_start - 1;
316 for (Paragraph::size_type lpos = bidi_start;
317 lpos <= bidi_end; ++lpos) {
318 vpos += log2vis_list[lpos - bidi_start];
319 vis2log_list[vpos - bidi_start] = lpos;
320 log2vis_list[lpos - bidi_start] = vpos;
325 // This method requires a previous call to ComputeBidiTables()
326 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
327 Paragraph::size_type pos) const
329 if (!lyxrc.rtl_support || pos == 0)
332 if (!bidi_InRange(pos - 1)) {
333 /// This can happen if pos is the first char of a row.
334 /// Returning false in this case is incorrect!
338 bool const rtl = bidi_level(pos - 1) % 2;
339 bool const rtl2 = bidi_InRange(pos)
340 ? bidi_level(pos) % 2
341 : par->isRightToLeftPar(buf->params);
346 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
347 Paragraph::size_type pos,
348 LyXFont const & font) const
350 if (!lyxrc.rtl_support)
351 return false; // This is just for speedup
353 bool const rtl = font.isVisibleRightToLeft();
354 bool const rtl2 = bidi_InRange(pos)
355 ? bidi_level(pos) % 2
356 : par->isRightToLeftPar(buf->params);
361 void LyXText::draw(BufferView * bview, Row const * row,
362 Paragraph::size_type & vpos,
363 int offset, float & x, bool cleared)
365 Painter & pain = bview->painter();
367 Paragraph::size_type pos = vis2log(vpos);
368 char c = row->par()->getChar(pos);
371 if (IsNewlineChar(c)) {
373 // Draw end-of-line marker
374 LyXFont const font = getFont(bview->buffer(), row->par(), pos);
375 int const wid = lyxfont::width('n', font);
376 int const asc = lyxfont::maxAscent(font);
377 int const y = offset + row->baseline();
381 if (bidi_level(pos) % 2 == 0) {
382 xp[0] = int(x + wid * 0.375);
383 yp[0] = int(y - 0.875 * asc * 0.75);
386 yp[1] = int(y - 0.500 * asc * 0.75);
388 xp[2] = int(x + wid * 0.375);
389 yp[2] = int(y - 0.125 * asc * 0.75);
391 pain.lines(xp, yp, 3, LColor::eolmarker);
394 yp[0] = int(y - 0.500 * asc * 0.75);
396 xp[1] = int(x + wid);
397 yp[1] = int(y - 0.500 * asc * 0.75);
399 xp[2] = int(x + wid);
400 yp[2] = int(y - asc * 0.75);
402 pain.lines(xp, yp, 3, LColor::eolmarker);
404 xp[0] = int(x + wid * 0.625);
405 yp[0] = int(y - 0.875 * asc * 0.75);
407 xp[1] = int(x + wid);
408 yp[1] = int(y - 0.500 * asc * 0.75);
410 xp[2] = int(x + wid * 0.625);
411 yp[2] = int(y - 0.125 * asc * 0.75);
413 pain.lines(xp, yp, 3, LColor::eolmarker);
415 xp[0] = int(x + wid);
416 yp[0] = int(y - 0.500 * asc * 0.75);
419 yp[1] = int(y - 0.500 * asc * 0.75);
422 yp[2] = int(y - asc * 0.75);
424 pain.lines(xp, yp, 3, LColor::eolmarker);
430 LyXFont font = getFont(bview->buffer(), row->par(), pos);
431 LyXFont font2 = font;
433 if (c == Paragraph::META_INSET) {
434 Inset * tmpinset = row->par()->getInset(pos);
436 tmpinset->update(bview, font, false);
437 tmpinset->draw(bview, font, offset+row->baseline(), x,
439 #ifdef SEEMS_TO_BE_NOT_NEEDED
440 if (status_ == CHANGED_IN_DRAW) {
441 UpdateInset(bview, tmpinset);
442 status(bview, CHANGED_IN_DRAW);
448 if (lyxrc.mark_foreign_language &&
449 font.language() != bview->buffer()->params.language) {
450 int const y = offset + row->height() - 1;
451 pain.line(int(tmpx), y, int(x), y,
458 /* usual characters, no insets */
460 // Collect character that we can draw in one command
462 // This is dirty, but fast. Notice that it will never be too small.
463 // For the record, I'll note that Microsoft Word has a limit
464 // of 768 here. We have none :-) (Asger)
465 // Ok. I am the first to admit that the use of std::string will be
466 // a tiny bit slower than using a POD char array. However, I claim
467 // that this slowdown is so small that it is close to inperceptive.
468 // So IMHO we should go with the easier and clearer implementation.
469 // And even if 1024 is a large number here it might overflow, string
470 // will only overflow if the machine is out of memory...
471 static string textstring;
475 Paragraph::size_type const last = rowLastPrintable(row);
477 if (font.language()->lang() == "hebrew") {
478 if (Encodings::IsComposeChar_hebrew(c)) {
479 int const width = lyxfont::width(c, font2);
481 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
482 c = row->par()->getChar(i);
483 if (!Encodings::IsComposeChar_hebrew(c)) {
484 if (IsPrintableNonspace(c)) {
489 dx = (c == 'ø' || c == 'ã') // dalet / resh
490 ? width2 - width : (width2 - width) / 2;
496 pain.text(int(x) + dx, offset + row->baseline(),
499 while (vpos <= last &&
500 (pos = vis2log(vpos)) >= 0
501 && IsPrintableNonspace(c = row->par()->getChar(pos))
502 && !Encodings::IsComposeChar_hebrew(c)
503 && font2 == getFont(bview->buffer(), row->par(), pos)) {
507 // Draw text and set the new x position
508 pain.text(int(x), offset + row->baseline(),
510 x += lyxfont::width(textstring, font);
512 } else if (font.language()->lang() == "arabic" &&
513 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
514 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
515 if (Encodings::IsComposeChar_arabic(c)) {
516 c = transformChar(c, row->par(), pos);
518 int const width = lyxfont::width(c, font2);
520 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
521 c = row->par()->getChar(i);
522 if (!Encodings::IsComposeChar_arabic(c)) {
523 if (IsPrintableNonspace(c)) {
528 dx = (width2 - width) / 2;
534 pain.text(int(x) + dx, offset + row->baseline(),
537 textstring = transformChar(c, row->par(), pos);
538 while (vpos <= last &&
539 (pos = vis2log(vpos)) >= 0
540 && IsPrintableNonspace(c = row->par()->getChar(pos))
541 && !Encodings::IsComposeChar_arabic(c)
542 && font2 == getFont(bview->buffer(), row->par(), pos)) {
543 c = transformChar(c, row->par(), pos);
547 // Draw text and set the new x position
548 pain.text(int(x), offset + row->baseline(),
550 x += lyxfont::width(textstring, font);
553 while (vpos <= last &&
554 (pos = vis2log(vpos)) >= 0
555 && IsPrintableNonspace(c = row->par()->getChar(pos))
556 && font2 == getFont(bview->buffer(), row->par(), pos)) {
560 // Draw text and set the new x position
561 pain.text(int(x), offset + row->baseline(), textstring, font);
562 x += lyxfont::width(textstring, font);
565 if (lyxrc.mark_foreign_language &&
566 font.language() != bview->buffer()->params.language) {
567 int const y = offset + row->height() - 1;
568 pain.line(int(tmpx), y, int(x), y,
572 // If we want ulem.sty support, drawing
573 // routines should go here. (Asger)
574 // Why shouldn't LyXFont::drawText handle it internally?
578 // Returns the left beginning of the text.
579 // This information cannot be taken from the layouts-objekt, because in
580 // LaTeX the beginning of the text fits in some cases (for example sections)
581 // exactly the label-width.
582 int LyXText::leftMargin(BufferView * bview, Row const * row) const
584 LyXLayout const & layout =
585 textclasslist.Style(bview->buffer()->params.textclass,
586 row->par()->getLayout());
588 string parindent = layout.parindent;
590 int x = LYX_PAPER_MARGIN;
592 x += lyxfont::signedWidth(textclasslist
593 .TextClass(bview->buffer()->params.textclass)
596 .TextClass(bview->buffer()->params.textclass)
599 // this is the way, LyX handles the LaTeX-Environments.
600 // I have had this idea very late, so it seems to be a
601 // later added hack and this is true
602 if (!row->par()->getDepth()) {
603 if (!row->par()->getLayout()) {
604 // find the previous same level paragraph
605 if (row->par()->previous()) {
606 Paragraph * newpar = row->par()
607 ->depthHook(row->par()->getDepth());
609 textclasslist.Style(bview->buffer()->params.textclass,
616 // find the next level paragraph
619 row->par()->outerHook();
621 // make a corresponding row. Needed to call LeftMargin()
623 // check wether it is a sufficent paragraph
626 .Style(bview->buffer()->params.textclass,
627 newpar->getLayout()).isEnvironment()) {
629 dummyrow.par(newpar);
630 dummyrow.pos(newpar->size());
631 x = leftMargin(bview, &dummyrow);
633 // this is no longer an error, because this function
634 // is used to clear impossible depths after changing
635 // a layout. Since there is always a redo,
636 // LeftMargin() is always called
637 row->par()->params().depth(0);
640 if (newpar && !row->par()->getLayout()) {
641 if (newpar->params().noindent())
644 parindent = textclasslist
645 .Style(bview->buffer()->params.textclass,
646 newpar->getLayout()).parindent;
651 LyXFont const labelfont = getFont(bview->buffer(), row->par(), -2);
652 switch (layout.margintype) {
654 if (!layout.leftmargin.empty()) {
655 x += lyxfont::signedWidth(layout.leftmargin,
657 .TextClass(bview->buffer()->params.
661 if (!row->par()->getLabelstring().empty()) {
662 x += lyxfont::signedWidth(layout.labelindent,
664 x += lyxfont::width(row->par()->getLabelstring(),
666 x += lyxfont::width(layout.labelsep, labelfont);
670 x += lyxfont::signedWidth(layout.labelindent, labelfont);
671 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
672 if (!row->par()->getLabelWidthString().empty()) {
673 x += lyxfont::width(row->par()->getLabelWidthString(),
675 x += lyxfont::width(layout.labelsep, labelfont);
680 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
681 / (row->par()->getDepth() + 4);
683 case MARGIN_FIRST_DYNAMIC:
684 if (layout.labeltype == LABEL_MANUAL) {
685 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
686 x += lyxfont::signedWidth(layout.leftmargin,
689 x += lyxfont::signedWidth(layout.labelindent,
692 } else if (row->pos()
693 // Special case to fix problems with
695 || (layout.labeltype == LABEL_STATIC
696 && layout.latextype == LATEX_ENVIRONMENT
697 && ! row->par()->isFirstInSequence())) {
698 x += lyxfont::signedWidth(layout.leftmargin,
700 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
701 && layout.labeltype != LABEL_BIBLIO
702 && layout.labeltype !=
703 LABEL_CENTERED_TOP_ENVIRONMENT) {
704 x += lyxfont::signedWidth(layout.labelindent,
706 x += lyxfont::width(layout.labelsep, labelfont);
707 x += lyxfont::width(row->par()->getLabelstring(),
712 case MARGIN_RIGHT_ADDRESS_BOX:
714 // ok, a terrible hack. The left margin depends on the widest
715 // row in this paragraph. Do not care about footnotes, they
716 // are *NOT* allowed in the LaTeX realisation of this layout.
718 // find the first row of this paragraph
719 Row const * tmprow = row;
720 while (tmprow->previous()
721 && tmprow->previous()->par() == row->par())
722 tmprow = tmprow->previous();
724 int minfill = tmprow->fill();
725 while (tmprow->next() && tmprow->next()->par() == row->par()) {
726 tmprow = tmprow->next();
727 if (tmprow->fill() < minfill)
728 minfill = tmprow->fill();
731 x += lyxfont::signedWidth(layout.leftmargin,
733 .TextClass(bview->buffer()->params.textclass)
740 int align; // wrong type
742 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
743 align = layout.align;
745 align = row->par()->params().align();
747 // set the correct parindent
748 if (row->pos() == 0) {
749 if ((layout.labeltype == LABEL_NO_LABEL
750 || layout.labeltype == LABEL_TOP_ENVIRONMENT
751 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
752 || (layout.labeltype == LABEL_STATIC
753 && layout.latextype == LATEX_ENVIRONMENT
754 && ! row->par()->isFirstInSequence()))
755 && align == LYX_ALIGN_BLOCK
756 && !row->par()->params().noindent()
757 && (row->par()->layout ||
758 bview->buffer()->params.paragraph_separation ==
759 BufferParams::PARSEP_INDENT))
760 x += lyxfont::signedWidth(parindent,
762 .TextClass(bview->buffer()->params
765 else if (layout.labeltype == LABEL_BIBLIO) {
766 // ale970405 Right width for bibitems
767 x += bibitemMaxWidth(bview,textclasslist
768 .TextClass(bview->buffer()->params
777 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
779 LyXLayout const & layout =
780 textclasslist.Style(buf->params.textclass,
781 row->par()->getLayout());
783 int x = LYX_PAPER_MARGIN
784 + lyxfont::signedWidth(textclasslist
785 .TextClass(buf->params.textclass)
788 .TextClass(buf->params.textclass)
791 // this is the way, LyX handles the LaTeX-Environments.
792 // I have had this idea very late, so it seems to be a
793 // later added hack and this is true
794 if (row->par()->getDepth()) {
795 // find the next level paragraph
797 Paragraph * newpar = row->par();
800 newpar = newpar->previous();
802 && newpar->getDepth() >= row->par()->getDepth());
804 // make a corresponding row. Needed to call LeftMargin()
806 // check wether it is a sufficent paragraph
808 && textclasslist.Style(buf->params.textclass,
812 dummyrow.par(newpar);
814 x = rightMargin(buf, &dummyrow);
816 // this is no longer an error, because this function
817 // is used to clear impossible depths after changing
818 // a layout. Since there is always a redo,
819 // LeftMargin() is always called
820 row->par()->params().depth(0);
824 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
825 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
826 .TextClass(buf->params.textclass)
827 .defaultfont()) * 4 / (row->par()->getDepth()
833 int LyXText::labelEnd(BufferView * bview, Row const * row) const
835 if (textclasslist.Style(bview->buffer()->params.textclass,
836 row->par()->getLayout()).margintype
840 tmprow.pos(row->par()->size());
841 return leftMargin(bview, &tmprow); /* just the beginning
844 return 0; /* LabelEnd is only needed, if the
845 layout fills a flushleft
850 // get the next breakpoint in a given paragraph
852 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
854 Paragraph * par = row->par();
859 Paragraph::size_type const pos = row->pos();
862 // position of the last possible breakpoint
863 // -1 isn't a suitable value, but a flag
864 Paragraph::size_type last_separator = -1;
865 width -= rightMargin(bview->buffer(), row);
867 Paragraph::size_type const main_body =
868 beginningOfMainBody(bview->buffer(), par);
869 LyXLayout const & layout =
870 textclasslist.Style(bview->buffer()->params.textclass, par->getLayout());
871 Paragraph::size_type i = pos;
873 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
874 /* special code for right address boxes, only newlines count */
875 while (i < par->size()) {
876 if (par->isNewline(i)) {
878 i = par->size() - 1; // this means break
880 } else if (par->getChar(i) == Paragraph::META_INSET &&
881 par->getInset(i) && par->getInset(i)->display()){
882 par->getInset(i)->display(false);
887 // Last position is an invariant
888 Paragraph::size_type const last =
890 // this is the usual handling
891 int x = leftMargin(bview, row);
892 bool doitonetime = true;
893 while (doitonetime || ((x < width) && (i < last))) {
895 char const c = par->getChar(i);
896 if (IsNewlineChar(c)) {
898 x = width; // this means break
899 } else if (c == Paragraph::META_INSET &&
902 // check wether a Display() inset is
903 // valid here. if not, change it to
905 if (par->getInset(i)->display() &&
906 (layout.isCommand() ||
907 (layout.labeltype == LABEL_MANUAL
908 && i < beginningOfMainBody(bview->buffer(), par)))) {
909 // display istn't allowd
910 par->getInset(i)->display(false);
911 x += singleWidth(bview, par, i, c);
912 } else if (par->getInset(i)->display() ||
913 par->getInset(i)->needFullRow()) {
914 // So break the line here
918 if (IsLineSeparatorChar(par->getChar(i+1)))
921 last_separator = last; // to avoid extra rows
923 last_separator = i - 1;
924 x = width; // this means break
926 x += singleWidth(bview, par, i, c);
929 if (IsLineSeparatorChar(c))
931 x += singleWidth(bview, par, i, c);
934 if (i == main_body) {
935 x += lyxfont::width(layout.labelsep,
936 getFont(bview->buffer(), par, -2));
937 if (par->isLineSeparator(i - 1))
938 x-= singleWidth(bview, par, i - 1);
939 int left_margin = labelEnd(bview, row);
944 // end of paragraph is always a suitable separator
945 if (i == last && x < width)
949 // well, if last_separator is still 0, the line isn't breakable.
950 // don't care and cut simply at the end
951 if (last_separator < 0) {
955 // manual labels cannot be broken in LaTeX, do not care
956 if (main_body && last_separator < main_body)
957 last_separator = main_body - 1;
959 return last_separator;
963 // returns the minimum space a row needs on the screen in pixel
964 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
970 // get the pure distance
971 Paragraph::size_type const last = rowLastPrintable(row);
973 // special handling of the right address boxes
974 if (textclasslist.Style(bview->buffer()->params.textclass,
975 row->par()->getLayout()).margintype
976 == MARGIN_RIGHT_ADDRESS_BOX) {
977 int const tmpfill = row->fill();
978 row->fill(0); // the minfill in MarginLeft()
979 w = leftMargin(bview, row);
982 w = leftMargin(bview, row);
984 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
985 row->par()->getLayout());
986 Paragraph::size_type const main_body =
987 beginningOfMainBody(bview->buffer(), row->par());
988 Paragraph::size_type i = row->pos();
991 if (main_body > 0 && i == main_body) {
992 w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
993 if (row->par()->isLineSeparator(i - 1))
994 w -= singleWidth(bview, row->par(), i - 1);
995 int left_margin = labelEnd(bview, row);
999 w += singleWidth(bview, row->par(), i);
1002 if (main_body > 0 && main_body > last) {
1003 w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
1004 if (last >= 0 && row->par()->isLineSeparator(last))
1005 w -= singleWidth(bview, row->par(), last);
1006 int const left_margin = labelEnd(bview, row);
1007 if (w < left_margin)
1011 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1012 #ifdef WITH_WARNINGS
1013 #warning Please fix me (Jug!)
1023 // returns the minimum space a manual label needs on the screen in pixel
1024 int LyXText::labelFill(BufferView * bview, Row const * row) const
1026 Paragraph::size_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1027 // -1 because a label ends either with a space that is in the label,
1028 // or with the beginning of a footnote that is outside the label.
1030 // I don't understand this code in depth, but sometimes "last" is
1031 // less than 0 and this causes a crash. This fix seems to work
1032 // correctly, but I bet the real error is elsewhere. The bug is
1033 // triggered when you have an open footnote in a paragraph
1034 // environment with a manual label. (Asger)
1035 if (last < 0) last = 0;
1037 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1044 w += singleWidth(bview, row->par(), i);
1049 if (!row->par()->params().labelWidthString().empty()) {
1050 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1051 getFont(bview->buffer(), row->par(), -2)) - w,
1059 // returns the number of separators in the specified row. The separator
1060 // on the very last column doesnt count
1061 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1063 Paragraph::size_type const last = rowLast(row);
1064 Paragraph::size_type p =
1065 max(row->pos(), beginningOfMainBody(buf, row->par()));
1067 for (; p < last; ++p) {
1068 if (row->par()->isSeparator(p)) {
1076 // returns the number of hfills in the specified row. The LyX-Hfill is
1077 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1078 // ignored. This is *MUCH* more usefull than not to ignore!
1079 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1081 Paragraph::size_type const last = rowLast(row);
1082 Paragraph::size_type first = row->pos();
1083 if (first) { /* hfill *DO* count at the beginning
1085 while(first <= last && row->par()->isHfill(first))
1089 first = max(first, beginningOfMainBody(buf, row->par()));
1091 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1092 if (row->par()->isHfill(p)) {
1100 // like NumberOfHfills, but only those in the manual label!
1101 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1103 Paragraph::size_type last = rowLast(row);
1104 Paragraph::size_type first = row->pos();
1105 if (first) { /* hfill *DO* count at the beginning
1107 while(first < last && row->par()->isHfill(first))
1111 last = min(last, beginningOfMainBody(buf, row->par()));
1113 for (Paragraph::size_type p = first;
1114 p < last; ++p) { // last, because the end is ignored!
1115 if (row->par()->isHfill(p)) {
1123 // returns true, if a expansion is needed.
1124 // Rules are given by LaTeX
1125 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1126 Paragraph::size_type pos) const
1128 // by the way, is it a hfill?
1129 if (!row_ptr->par()->isHfill(pos))
1132 // at the end of a row it does not count
1133 if (pos >= rowLast(row_ptr))
1136 // at the beginning of a row it does not count, if it is not
1137 // the first row of a paragaph
1138 if (!row_ptr->pos())
1141 // in some labels it does not count
1142 if (textclasslist.Style(buf->params.textclass,
1143 row_ptr->par()->getLayout()).margintype
1145 && pos < beginningOfMainBody(buf, row_ptr->par()))
1148 // if there is anything between the first char of the row and
1149 // the sepcified position that is not a newline and not a hfill,
1150 // the hfill will count, otherwise not
1151 Paragraph::size_type i = row_ptr->pos();
1152 while (i < pos && (row_ptr->par()->isNewline(i)
1153 || row_ptr->par()->isHfill(i)))
1160 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1162 /* get the maximum ascent and the maximum descent */
1165 float layoutasc = 0;
1166 float layoutdesc = 0;
1169 Inset * tmpinset = 0;
1171 /* this must not happen before the currentrow for clear reasons.
1172 so the trick is just to set the current row onto this row */
1174 getRow(row_ptr->par(), row_ptr->pos(), unused_y);
1176 /* ok , let us initialize the maxasc and maxdesc value.
1177 * This depends in LaTeX of the font of the last character
1178 * in the paragraph. The hack below is necessary because
1179 * of the possibility of open footnotes */
1181 /* Correction: only the fontsize count. The other properties
1182 are taken from the layoutfont. Nicer on the screen :) */
1183 Paragraph * par = row_ptr->par();
1184 Paragraph * firstpar = row_ptr->par();
1186 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1187 firstpar->getLayout());
1189 LyXFont font = getFont(bview->buffer(), par, par->size() - 1);
1190 LyXFont::FONT_SIZE const size = font.size();
1191 font = getFont(bview->buffer(), par, -1);
1194 LyXFont labelfont = getFont(bview->buffer(), par, -2);
1196 float spacing_val = 1.0;
1197 if (!row_ptr->par()->params().spacing().isDefault()) {
1198 spacing_val = row_ptr->par()->params().spacing().getValue();
1200 spacing_val = bview->buffer()->params.spacing.getValue();
1202 //lyxerr << "spacing_val = " << spacing_val << endl;
1204 int maxasc = int(lyxfont::maxAscent(font) *
1205 layout.spacing.getValue() *
1207 int maxdesc = int(lyxfont::maxDescent(font) *
1208 layout.spacing.getValue() *
1210 int const pos_end = rowLast(row_ptr);
1214 // Check if any insets are larger
1215 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1216 if (row_ptr->par()->getChar(pos) == Paragraph::META_INSET) {
1217 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1218 tmpinset = row_ptr->par()->getInset(pos);
1220 #if 1 // this is needed for deep update on initialitation
1221 tmpinset->update(bview, tmpfont);
1223 asc = tmpinset->ascent(bview, tmpfont);
1224 desc = tmpinset->descent(bview, tmpfont);
1225 maxwidth += tmpinset->width(bview, tmpfont);
1226 maxasc = max(maxasc, asc);
1227 maxdesc = max(maxdesc, desc);
1230 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1234 // Check if any custom fonts are larger (Asger)
1235 // This is not completely correct, but we can live with the small,
1236 // cosmetic error for now.
1237 LyXFont::FONT_SIZE const maxsize =
1238 row_ptr->par()->highestFontInRange(row_ptr->pos(),
1240 if (maxsize > font.size()) {
1241 font.setSize(maxsize);
1243 asc = lyxfont::maxAscent(font);
1244 desc = lyxfont::maxDescent(font);
1251 // This is nicer with box insets:
1255 row_ptr->ascent_of_text(maxasc);
1257 // is it a top line?
1258 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1260 // some parksips VERY EASY IMPLEMENTATION
1261 if (bview->buffer()->params.paragraph_separation ==
1262 BufferParams::PARSEP_SKIP) {
1263 if (layout.isParagraph()
1264 && firstpar->getDepth() == 0
1265 && firstpar->previous())
1266 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1267 else if (firstpar->previous()
1268 && textclasslist.Style(bview->buffer()->params.textclass,
1269 firstpar->previous()->getLayout()).isParagraph()
1270 && firstpar->previous()->getDepth() == 0)
1271 // is it right to use defskip here too? (AS)
1272 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1275 // the paper margins
1276 if (!row_ptr->par()->previous() && bv_owner)
1277 maxasc += LYX_PAPER_MARGIN;
1279 // add the vertical spaces, that the user added
1280 if (firstpar->params().spaceTop().kind() != VSpace::NONE)
1281 maxasc += int(firstpar->params().spaceTop().inPixels(bview));
1283 // do not forget the DTP-lines!
1284 // there height depends on the font of the nearest character
1285 if (firstpar->params().lineTop())
1286 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1289 // and now the pagebreaks
1290 if (firstpar->params().pagebreakTop())
1291 maxasc += 3 * defaultHeight();
1293 // This is special code for the chapter, since the label of this
1294 // layout is printed in an extra row
1295 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1296 && bview->buffer()->params.secnumdepth >= 0) {
1297 float spacing_val = 1.0;
1298 if (!row_ptr->par()->params().spacing().isDefault()) {
1299 spacing_val = row_ptr->par()->params().spacing().getValue();
1301 spacing_val = bview->buffer()->params.spacing.getValue();
1304 labeladdon = int(lyxfont::maxDescent(labelfont) *
1305 layout.spacing.getValue() *
1307 + int(lyxfont::maxAscent(labelfont) *
1308 layout.spacing.getValue() *
1312 // special code for the top label
1313 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1314 || layout.labeltype == LABEL_BIBLIO
1315 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1316 && row_ptr->par()->isFirstInSequence()
1317 && !row_ptr->par()->getLabelstring().empty()) {
1318 float spacing_val = 1.0;
1319 if (!row_ptr->par()->params().spacing().isDefault()) {
1320 spacing_val = row_ptr->par()->params().spacing().getValue();
1322 spacing_val = bview->buffer()->params.spacing.getValue();
1326 (lyxfont::maxAscent(labelfont) *
1327 layout.spacing.getValue() *
1329 +(lyxfont::maxDescent(labelfont) *
1330 layout.spacing.getValue() *
1332 + layout.topsep * defaultHeight()
1333 + layout.labelbottomsep * defaultHeight());
1336 // and now the layout spaces, for example before and after a section,
1337 // or between the items of a itemize or enumerate environment
1339 if (!firstpar->params().pagebreakTop()) {
1340 Paragraph * prev = row_ptr->par()->previous();
1342 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1343 if (prev && prev->getLayout() == firstpar->getLayout()
1344 && prev->getDepth() == firstpar->getDepth()
1345 && prev->getLabelWidthString() == firstpar->getLabelWidthString())
1347 layoutasc = (layout.itemsep * defaultHeight());
1349 else if (row_ptr->previous()) {
1350 tmptop = layout.topsep;
1352 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1353 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1354 row_ptr->previous()->par()->
1355 getLayout()).bottomsep;
1358 layoutasc = (tmptop * defaultHeight());
1360 else if (row_ptr->par()->params().lineTop()) {
1361 tmptop = layout.topsep;
1364 layoutasc = (tmptop * defaultHeight());
1367 prev = row_ptr->par()->outerHook();
1369 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1370 prev->getLayout()).parsep * defaultHeight());
1373 if (firstpar->previous()
1374 && firstpar->previous()->getDepth() == 0
1375 && firstpar->previous()->getLayout() != firstpar->getLayout()) {
1378 else if (firstpar->previous()){
1379 maxasc += int(layout.parsep * defaultHeight());
1385 // is it a bottom line?
1386 if (row_ptr->par() == par
1387 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
1389 // the paper margins
1390 if (!par->next() && bv_owner)
1391 maxdesc += LYX_PAPER_MARGIN;
1393 // add the vertical spaces, that the user added
1394 if (firstpar->params().spaceBottom().kind() != VSpace::NONE)
1395 maxdesc += int(firstpar->params().spaceBottom().inPixels(bview));
1397 // do not forget the DTP-lines!
1398 // there height depends on the font of the nearest character
1399 if (firstpar->params().lineBottom())
1400 maxdesc += 2 * lyxfont::ascent('x',
1401 getFont(bview->buffer(),
1402 par, par->size() - 1));
1404 // and now the pagebreaks
1405 if (firstpar->params().pagebreakBottom())
1406 maxdesc += 3 * defaultHeight();
1408 // and now the layout spaces, for example before and after
1409 // a section, or between the items of a itemize or enumerate
1411 if (!firstpar->params().pagebreakBottom() && row_ptr->par()->next()) {
1412 Paragraph * nextpar = row_ptr->par()->next();
1413 Paragraph * comparepar = row_ptr->par();
1417 if (comparepar->getDepth() > nextpar->getDepth()) {
1418 usual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1419 comparepar = comparepar->depthHook(nextpar->getDepth());
1420 if (comparepar->getLayout()!= nextpar->getLayout()
1421 || nextpar->getLabelWidthString() !=
1422 comparepar->getLabelWidthString())
1423 unusual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1425 if (unusual > usual)
1426 layoutdesc = unusual;
1430 else if (comparepar->getDepth() == nextpar->getDepth()) {
1432 if (comparepar->getLayout()!= nextpar->getLayout()
1433 || nextpar->getLabelWidthString() !=
1434 comparepar->getLabelWidthString())
1435 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1440 // incalculate the layout spaces
1441 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1442 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1444 // calculate the new height of the text
1445 height -= row_ptr->height();
1447 row_ptr->height(maxasc + maxdesc + labeladdon);
1448 row_ptr->baseline(maxasc + labeladdon);
1450 height += row_ptr->height();
1453 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1454 row_ptr->width(int(maxwidth + x));
1457 width = max(0,workWidth(bview));
1459 if (r->width() > width)
1467 /* Appends the implicit specified paragraph behind the specified row,
1468 * start at the implicit given position */
1469 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1471 bool not_ready = true;
1473 // The last character position of a paragraph is an invariant so we can
1474 // safely get it here. (Asger)
1475 int const lastposition = row->par()->size();
1477 // Get the next breakpoint
1478 int z = nextBreakPoint(bview, row, workWidth(bview));
1482 // Insert the new row
1483 if (z < lastposition) {
1485 insertRow(row, row->par(), z);
1492 // Set the dimensions of the row
1493 #ifdef WITH_WARNINGS
1494 #warning Something is rotten here! (Jug)
1496 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1497 setHeightOfRow(bview, tmprow);
1499 } while (not_ready);
1503 void LyXText::breakAgain(BufferView * bview, Row * row) const
1505 bool not_ready = true;
1508 // get the next breakpoint
1509 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1512 if (z < row->par()->size()) {
1513 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1516 insertRow(row, row->par(), z);
1522 if (row->pos() == z)
1523 not_ready = false; // the rest will not change
1529 /* if there are some rows too much, delete them */
1530 /* only if you broke the whole paragraph! */
1531 Row * tmprow2 = row;
1532 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1533 tmprow2 = tmprow2->next();
1535 while (tmprow2 != row) {
1536 tmprow2 = tmprow2->previous();
1537 removeRow(tmprow2->next());
1542 /* set the dimensions of the row */
1543 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1544 setHeightOfRow(bview, tmprow);
1545 } while (not_ready);
1549 // this is just a little changed version of break again
1550 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1552 // get the next breakpoint
1553 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1556 if (z < row->par()->size()) {
1558 || (row->next() && row->next()->par() != row->par())) {
1559 /* insert a new row */
1561 insertRow(row, row->par(), z);
1567 if (row->pos() != z)
1571 // if there are some rows too much, delete them
1572 // only if you broke the whole paragraph!
1573 Row * tmprow2 = row;
1574 while (tmprow2->next()
1575 && tmprow2->next()->par() == row->par()) {
1576 tmprow2 = tmprow2->next();
1578 while (tmprow2 != row) {
1579 tmprow2 = tmprow2->previous();
1580 removeRow(tmprow2->next());
1584 // set the dimensions of the row
1585 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1586 setHeightOfRow(bview, tmprow);
1590 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1592 LyXLayout const & layout =
1593 textclasslist.Style(bview->buffer()->params.textclass,
1594 cursor.par()->getLayout());
1596 // this is only allowed, if the current paragraph is not empty or caption
1597 if ((cursor.par()->size() <= 0)
1598 && layout.labeltype!= LABEL_SENSITIVE)
1601 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1603 // Always break behind a space
1605 // It is better to erase the space (Dekel)
1606 if (cursor.pos() < cursor.par()->size()
1607 && cursor.par()->isLineSeparator(cursor.pos()))
1608 cursor.par()->erase(cursor.pos());
1609 // cursor.pos(cursor.pos() + 1);
1611 // break the paragraph
1615 keep_layout = layout.isEnvironment();
1616 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1619 // well this is the caption hack since one caption is really enough
1620 if (layout.labeltype == LABEL_SENSITIVE) {
1622 // set to standard-layout
1623 cursor.par()->setLayout(0);
1625 // set to standard-layout
1626 cursor.par()->next()->setLayout(0);
1629 /* if the cursor is at the beginning of a row without prior newline,
1631 * This touches only the screen-update. Otherwise we would may have
1632 * an empty row on the screen */
1633 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1634 && cursor.row()->pos() == cursor.pos()) {
1638 status(bview, LyXText::NEED_MORE_REFRESH);
1639 refresh_row = cursor.row();
1640 refresh_y = cursor.y() - cursor.row()->baseline();
1642 // Do not forget the special right address boxes
1643 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1644 while (refresh_row->previous() &&
1645 refresh_row->previous()->par() == refresh_row->par()) {
1646 refresh_row = refresh_row->previous();
1647 refresh_y -= refresh_row->height();
1650 removeParagraph(cursor.row());
1652 // set the dimensions of the cursor row
1653 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1655 setHeightOfRow(bview, cursor.row());
1657 while (cursor.par()->next()->size()
1658 && cursor.par()->next()->isNewline(0))
1659 cursor.par()->next()->erase(0);
1661 insertParagraph(bview, cursor.par()->next(), cursor.row());
1663 updateCounters(bview, cursor.row()->previous());
1665 /* This check is necessary. Otherwise the new empty paragraph will
1666 * be deleted automatically. And it is more friendly for the user! */
1668 setCursor(bview, cursor.par()->next(), 0);
1670 setCursor(bview, cursor.par(), 0);
1672 if (cursor.row()->next())
1673 breakAgain(bview, cursor.row()->next());
1679 // Just a macro to make some thing easier.
1680 void LyXText::redoParagraph(BufferView * bview) const
1682 clearSelection(bview);
1683 redoParagraphs(bview, cursor, cursor.par()->next());
1684 setCursorIntern(bview, cursor.par(), cursor.pos());
1688 /* insert a character, moves all the following breaks in the
1689 * same Paragraph one to the right and make a rebreak */
1690 void LyXText::insertChar(BufferView * bview, char c)
1692 setUndo(bview, Undo::INSERT,
1693 cursor.par(), cursor.par()->next());
1695 // When the free-spacing option is set for the current layout,
1696 // disable the double-space checking
1698 bool const freeSpacing =
1699 textclasslist.Style(bview->buffer()->params.textclass,
1700 cursor.row()->par()->getLayout()).free_spacing;
1703 if (lyxrc.auto_number) {
1704 static string const number_operators = "+-/*";
1705 static string const number_unary_operators = "+-";
1706 static string const number_seperators = ".,:";
1708 if (current_font.number() == LyXFont::ON) {
1709 if (!isdigit(c) && !contains(number_operators, c) &&
1710 !(contains(number_seperators, c) &&
1711 cursor.pos() >= 1 &&
1712 cursor.pos() < cursor.par()->size() &&
1713 getFont(bview->buffer(),
1715 cursor.pos()).number() == LyXFont::ON &&
1716 getFont(bview->buffer(),
1718 cursor.pos()-1).number() == LyXFont::ON)
1720 Number(bview); // Set current_font.number to OFF
1721 } else if (isdigit(c) &&
1722 real_current_font.isVisibleRightToLeft()) {
1723 Number(bview); // Set current_font.number to ON
1725 if (cursor.pos() > 0) {
1726 char const c = cursor.par()->getChar(cursor.pos() - 1);
1727 if (contains(number_unary_operators, c) &&
1728 (cursor.pos() == 1 ||
1729 cursor.par()->isSeparator(cursor.pos() - 2) ||
1730 cursor.par()->isNewline(cursor.pos() - 2) )
1732 setCharFont(bview->buffer(),
1736 } else if (contains(number_seperators, c) &&
1737 cursor.pos() >= 2 &&
1738 getFont(bview->buffer(),
1740 cursor.pos()-2).number() == LyXFont::ON) {
1741 setCharFont(bview->buffer(),
1751 /* First check, if there will be two blanks together or a blank at
1752 the beginning of a paragraph.
1753 I decided to handle blanks like normal characters, the main
1754 difference are the special checks when calculating the row.fill
1755 (blank does not count at the end of a row) and the check here */
1757 // The bug is triggered when we type in a description environment:
1758 // The current_font is not changed when we go from label to main text
1759 // and it should (along with realtmpfont) when we type the space.
1760 // CHECK There is a bug here! (Asger)
1762 LyXFont realtmpfont = real_current_font;
1763 LyXFont rawtmpfont = current_font; /* store the current font.
1764 * This is because of the use
1765 * of cursor movements. The moving
1766 * cursor would refresh the
1769 // Get the font that is used to calculate the baselineskip
1770 Paragraph::size_type const lastpos = cursor.par()->size();
1771 LyXFont rawparfont =
1772 cursor.par()->getFontSettings(bview->buffer()->params,
1775 bool jumped_over_space = false;
1777 if (!freeSpacing && IsLineSeparatorChar(c)) {
1778 if ((cursor.pos() > 0
1779 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1780 || (cursor.pos() > 0
1781 && cursor.par()->isNewline(cursor.pos() - 1))
1782 || (cursor.pos() == 0)) {
1783 static bool sent_space_message = false;
1784 if (!sent_space_message) {
1785 if (cursor.pos() == 0)
1786 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1788 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1789 sent_space_message = true;
1794 } else if (IsNewlineChar(c)) {
1795 if (cursor.par() == cursor.par()
1796 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1800 /* No newline at first position
1801 * of a paragraph or behind labels.
1802 * TeX does not allow that. */
1804 if (cursor.pos() < cursor.par()->size() &&
1805 cursor.par()->isLineSeparator(cursor.pos()))
1806 // newline always after a blank!
1808 cursor.row()->fill(-1); // to force a new break
1811 // the display inset stuff
1812 if (cursor.row()->par()->getChar(cursor.row()->pos()) == Paragraph::META_INSET
1813 && cursor.row()->par()->getInset(cursor.row()->pos())
1814 && (cursor.row()->par()->getInset(cursor.row()->pos())->display() ||
1815 cursor.row()->par()->getInset(cursor.row()->pos())->needFullRow()))
1816 cursor.row()->fill(-1); // to force a new break
1818 // get the cursor row fist
1819 Row * row = cursor.row();
1820 int y = cursor.y() - row->baseline();
1821 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1822 * already insertet the character */
1823 cursor.par()->insertChar(cursor.pos(), c);
1824 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1826 if (!jumped_over_space) {
1827 // refresh the positions
1829 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1830 tmprow = tmprow->next();
1831 tmprow->pos(tmprow->pos() + 1);
1835 // Is there a break one row above
1836 if ((cursor.par()->isLineSeparator(cursor.pos())
1837 || cursor.par()->isNewline(cursor.pos())
1838 || cursor.row()->fill() == -1)
1839 && row->previous() && row->previous()->par() == row->par()) {
1840 Paragraph::size_type z = nextBreakPoint(bview,
1843 if (z >= row->pos()) {
1846 // set the dimensions of the row above
1847 row->previous()->fill(fill(bview,
1851 setHeightOfRow(bview, row->previous());
1853 y -= row->previous()->height();
1855 refresh_row = row->previous();
1856 status(bview, LyXText::NEED_MORE_REFRESH);
1858 breakAgainOneRow(bview, row);
1860 current_font = rawtmpfont;
1861 real_current_font = realtmpfont;
1862 setCursor(bview, cursor.par(), cursor.pos() + 1,
1863 false, cursor.boundary());
1864 // cursor MUST be in row now.
1866 if (row->next() && row->next()->par() == row->par())
1867 need_break_row = row->next();
1871 // check, wether the last characters font has changed.
1872 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1873 && rawparfont != rawtmpfont)
1874 redoHeightOfParagraph(bview, cursor);
1881 // recalculate the fill of the row
1882 if (row->fill() >= 0) /* needed because a newline
1883 * will set fill to -1. Otherwise
1884 * we would not get a rebreak! */
1885 row->fill(fill(bview, row, workWidth(bview)));
1886 if (row->fill() < 0) {
1889 refresh_x = cursor.x();
1890 refresh_pos = cursor.pos();
1891 status(bview, LyXText::NEED_MORE_REFRESH);
1892 breakAgainOneRow(bview, row);
1893 // will the cursor be in another row now?
1894 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1895 if (row->next() && row->next()->par() == row->par())
1896 // this should always be true
1898 breakAgainOneRow(bview, row);
1900 current_font = rawtmpfont;
1901 real_current_font = realtmpfont;
1903 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1905 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1906 != cursor.boundary())
1907 setCursor(bview, cursor.par(), cursor.pos(), false,
1908 !cursor.boundary());
1909 if (row->next() && row->next()->par() == row->par())
1910 need_break_row = row->next();
1915 refresh_x = cursor.x();
1917 refresh_pos = cursor.pos();
1919 int const tmpheight = row->height();
1920 setHeightOfRow(bview, row);
1921 if (tmpheight == row->height())
1922 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
1924 status(bview, LyXText::NEED_MORE_REFRESH);
1926 current_font = rawtmpfont;
1927 real_current_font = realtmpfont;
1928 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1932 // check, wether the last characters font has changed.
1933 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1934 && rawparfont != rawtmpfont) {
1935 redoHeightOfParagraph(bview, cursor);
1937 // now the special right address boxes
1938 if (textclasslist.Style(bview->buffer()->params.textclass,
1939 cursor.par()->getLayout()).margintype
1940 == MARGIN_RIGHT_ADDRESS_BOX) {
1941 redoDrawingOfParagraph(bview, cursor);
1949 void LyXText::charInserted()
1951 // Here we could call FinishUndo for every 20 characters inserted.
1952 // This is from my experience how emacs does it.
1953 static unsigned int counter;
1963 void LyXText::prepareToPrint(BufferView * bview,
1964 Row * row, float & x,
1965 float & fill_separator,
1967 float & fill_label_hfill,
1973 float w = row->fill();
1975 fill_label_hfill = 0;
1977 fill_label_hfill = 0;
1980 row->par()->isRightToLeftPar(bview->buffer()->params);
1982 x = (workWidth(bview) > 0)
1983 ? rightMargin(bview->buffer(), row) : 0;
1985 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
1987 // is there a manual margin with a manual label
1988 if (textclasslist.Style(bview->buffer()->params.textclass,
1989 row->par()->getLayout()).margintype == MARGIN_MANUAL
1990 && textclasslist.Style(bview->buffer()->params.textclass,
1991 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
1993 /* one more since labels are left aligned */
1994 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
1995 if (nlh && !row->par()->getLabelWidthString().empty()) {
1996 fill_label_hfill = labelFill(bview, row) / nlh;
2000 // are there any hfills in the row?
2001 float const nh = numberOfHfills(bview->buffer(), row);
2004 fill_hfill = w / nh;
2006 // is it block, flushleft or flushright?
2007 // set x how you need it
2009 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
2010 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2012 align = row->par()->params().align();
2014 // center displayed insets
2016 if (row->par()->getChar(row->pos()) == Paragraph::META_INSET
2017 && (inset=row->par()->getInset(row->pos()))
2018 && (inset->display())) // || (inset->scroll() < 0)))
2019 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2020 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2023 case LYX_ALIGN_BLOCK:
2024 ns = numberOfSeparators(bview->buffer(), row);
2025 if (ns && row->next() && row->next()->par() == row->par() &&
2026 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2027 && !(row->next()->par()->getChar(row->next()->pos()) == Paragraph::META_INSET
2028 && row->next()->par()->getInset(row->next()->pos())
2029 && row->next()->par()->getInset(row->next()->pos())->display())
2031 fill_separator = w / ns;
2035 case LYX_ALIGN_RIGHT:
2038 case LYX_ALIGN_CENTER:
2046 computeBidiTables(bview->buffer(), row);
2048 Paragraph::size_type main_body =
2049 beginningOfMainBody(bview->buffer(), row->par());
2050 Paragraph::size_type last = rowLast(row);
2052 if (main_body > 0 &&
2053 (main_body-1 > last ||
2054 !row->par()->isLineSeparator(main_body-1))) {
2055 LyXLayout const & layout =
2056 textclasslist.Style(bview->buffer()->params.textclass,
2057 row->par()->getLayout());
2058 x += lyxfont::width(layout.labelsep,
2059 getFont(bview->buffer(), row->par(), -2));
2060 if (main_body-1 <= last)
2061 x += fill_label_hfill;
2066 /* important for the screen */
2069 /* the cursor set functions have a special mechanism. When they
2070 * realize, that you left an empty paragraph, they will delete it.
2071 * They also delete the corresponding row */
2073 void LyXText::cursorRightOneWord(BufferView * bview) const
2075 // treat floats, HFills and Insets as words
2076 LyXCursor tmpcursor = cursor;
2077 // CHECK See comment on top of text.C
2079 if (tmpcursor.pos() == tmpcursor.par()->size()
2080 && tmpcursor.par()->next()) {
2081 tmpcursor.par(tmpcursor.par()->next());
2086 // Skip through initial nonword stuff.
2087 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2088 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2089 // printf("Current pos1 %d", tmpcursor.pos()) ;
2090 tmpcursor.pos(tmpcursor.pos() + 1);
2093 // Advance through word.
2094 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2095 tmpcursor.par()->isWord( tmpcursor.pos())) {
2096 // printf("Current pos2 %d", tmpcursor.pos()) ;
2097 tmpcursor.pos(tmpcursor.pos() + 1);
2101 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2105 void LyXText::cursorTab(BufferView * bview) const
2107 LyXCursor tmpcursor = cursor;
2108 while (tmpcursor.pos() < tmpcursor.par()->size()
2109 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2110 tmpcursor.pos(tmpcursor.pos() + 1);
2112 if (tmpcursor.pos() == tmpcursor.par()->size()){
2113 if (tmpcursor.par()->next()) {
2114 tmpcursor.par(tmpcursor.par()->next());
2118 tmpcursor.pos(tmpcursor.pos() + 1);
2119 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2123 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2124 of prior word, not to end of next prior word. */
2126 void LyXText::cursorLeftOneWord(BufferView * bview) const
2128 LyXCursor tmpcursor = cursor;
2129 cursorLeftOneWord(tmpcursor);
2130 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2133 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2135 // treat HFills, floats and Insets as words
2138 && (cur.par()->isSeparator(cur.pos() - 1)
2139 || cur.par()->isKomma(cur.pos() - 1))
2140 && !(cur.par()->isHfill(cur.pos() - 1)
2141 || cur.par()->isInset(cur.pos() - 1)))
2142 cur.pos(cur.pos() - 1);
2145 && (cur.par()->isInset(cur.pos() - 1)
2146 || cur.par()->isHfill(cur.pos() - 1))) {
2147 cur.pos(cur.pos() - 1);
2148 } else if (!cur.pos()) {
2149 if (cur.par()->previous()){
2150 cur.par(cur.par()->previous());
2151 cur.pos(cur.par()->size());
2153 } else { // Here, cur != 0
2154 while (cur.pos() > 0 &&
2155 cur.par()->isWord(cur.pos()-1) )
2156 cur.pos(cur.pos() - 1);
2160 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
2163 void LyXText::getWord(LyXCursor & from, LyXCursor & to, word_location loc) const
2165 // first put the cursor where we wana start to select the word
2169 // Move cursor to the beginning, when not already there.
2170 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2171 && !from.par()->isKomma(from.pos() - 1))
2172 cursorLeftOneWord(from);
2175 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2181 while (to.pos() < to.par()->size()
2182 && !to.par()->isSeparator(to.pos())
2183 && !to.par()->isKomma(to.pos())
2184 && !to.par()->isHfill(to.pos()) )
2186 to.pos(to.pos() + 1);
2191 void LyXText::selectWord(BufferView * bview)
2195 getWord(from, to, WHOLE_WORD);
2197 setCursor(bview, from.par(), from.pos());
2198 selection.cursor = cursor;
2199 setCursor(bview, to.par(), to.pos() );
2200 setSelection(bview);
2203 /* -------> Select the word currently under the cursor when:
2204 1: no selection is currently set,
2205 2: the cursor is not at the borders of the word. */
2207 bool LyXText::selectWordWhenUnderCursor(BufferView * bview)
2209 if (!selection.set() &&
2210 cursor.pos() > 0 && cursor.pos() < cursor.par()->size()
2211 && !cursor.par()->isSeparator(cursor.pos())
2212 && !cursor.par()->isKomma(cursor.pos())
2213 && !cursor.par()->isSeparator(cursor.pos() -1)
2214 && !cursor.par()->isKomma(cursor.pos() -1)) {
2222 // This function is only used by the spellchecker for NextWord().
2223 // It doesn't handle LYX_ACCENTs and probably never will.
2224 string const LyXText::selectNextWord(BufferView * bview,
2225 float & value) const
2227 if (the_locking_inset) {
2228 string str = the_locking_inset->selectNextWord(bview, value);
2230 value += float(cursor.y())/float(height);
2233 #warning Dekel please have a look on this one RTL? (Jug)
2234 // we have to go on checking so move cusor to the right
2235 if (cursor.pos() == cursor.par()->size()) {
2236 if (!cursor.par()->next())
2238 cursor.par(cursor.par()->next());
2241 cursor.pos(cursor.pos() + 1);
2243 Paragraph * tmppar = cursor.par();
2245 // If this is not the very first word, skip rest of
2246 // current word because we are probably in the middle
2247 // of a word if there is text here.
2248 if (cursor.pos() || cursor.par()->previous()) {
2249 while (cursor.pos() < cursor.par()->size()
2250 && cursor.par()->isLetter(cursor.pos()))
2251 cursor.pos(cursor.pos() + 1);
2254 // Now, skip until we have real text (will jump paragraphs)
2255 while ((cursor.par()->size() > cursor.pos()
2256 && (!cursor.par()->isLetter(cursor.pos())
2258 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2259 .latex() == LyXFont::ON
2262 && (!cursor.par()->isInset(cursor.pos()) ||
2263 !cursor.par()->getInset(cursor.pos())->isTextInset()))
2264 || (cursor.par()->size() == cursor.pos()
2265 && cursor.par()->next()))
2267 if (cursor.pos() == cursor.par()->size()) {
2268 cursor.par(cursor.par()->next());
2271 cursor.pos(cursor.pos() + 1);
2274 // now check if we hit an inset so it has to be a inset containing text!
2275 if (cursor.pos() < cursor.par()->size() &&
2276 cursor.par()->isInset(cursor.pos()))
2279 cursor.par()->getInset(cursor.pos())->edit(bview);
2280 // now call us again to do the above trick
2281 // but obviously we have to start from down below ;)
2282 return bview->text->selectNextWord(bview, value);
2285 // Update the value if we changed paragraphs
2286 if (cursor.par() != tmppar){
2287 setCursor(bview, cursor.par(), cursor.pos());
2288 value = float(cursor.y())/float(height);
2291 // Start the selection from here
2292 selection.cursor = cursor;
2294 ostringstream latex;
2296 // and find the end of the word
2297 // (optional hyphens are part of a word)
2298 while (cursor.pos() < cursor.par()->size()
2299 && (cursor.par()->isLetter(cursor.pos()))
2300 || (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET
2301 && cursor.par()->getInset(cursor.pos()) != 0
2302 && cursor.par()->getInset(cursor.pos())->latex(bview->buffer(), latex, false, false) == 0
2303 && latex.str() == "\\-"
2305 cursor.pos(cursor.pos() + 1);
2307 // Finally, we copy the word to a string and return it
2309 if (selection.cursor.pos() < cursor.pos()) {
2310 Paragraph::size_type i;
2311 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2312 if (cursor.par()->getChar(i) != Paragraph::META_INSET)
2313 str += cursor.par()->getChar(i);
2320 // This one is also only for the spellchecker
2321 void LyXText::selectSelectedWord(BufferView * bview)
2323 if (the_locking_inset) {
2324 the_locking_inset->selectSelectedWord(bview);
2327 // move cursor to the beginning
2328 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2330 // set the sel cursor
2331 selection.cursor = cursor;
2332 ostringstream latex;
2334 // now find the end of the word
2335 while (cursor.pos() < cursor.par()->size()
2336 && (cursor.par()->isLetter(cursor.pos())
2337 || (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET
2338 && cursor.par()->getInset(cursor.pos()) != 0
2339 && cursor.par()->getInset(cursor.pos())->latex(bview->buffer(), latex, false, false) == 0
2340 && latex.str() == "\\-"
2342 cursor.pos(cursor.pos() + 1);
2344 setCursor(bview, cursor.par(), cursor.pos());
2346 // finally set the selection
2347 setSelection(bview);
2351 /* -------> Delete from cursor up to the end of the current or next word. */
2352 void LyXText::deleteWordForward(BufferView * bview)
2354 if (!cursor.par()->size())
2357 LyXCursor tmpcursor = cursor;
2358 tmpcursor.row(0); //Â ??
2359 selection.set(true); // to avoid deletion
2360 cursorRightOneWord(bview);
2361 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2362 selection.cursor = cursor;
2364 setSelection(bview);
2366 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2367 cutSelection(bview);
2372 /* -------> Delete from cursor to start of current or prior word. */
2373 void LyXText::deleteWordBackward(BufferView * bview)
2375 if (!cursor.par()->size())
2378 LyXCursor tmpcursor = cursor;
2379 tmpcursor.row(0); // ??
2380 selection.set(true); // to avoid deletion
2381 cursorLeftOneWord(bview);
2382 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2383 selection.cursor = cursor;
2385 setSelection(bview);
2386 cutSelection(bview);
2391 /* -------> Kill to end of line. */
2392 void LyXText::deleteLineForward(BufferView * bview)
2394 if (!cursor.par()->size())
2395 // Paragraph is empty, so we just go to the right
2398 LyXCursor tmpcursor = cursor;
2399 // We can't store the row over a regular setCursor
2400 // so we set it to 0 and reset it afterwards.
2401 tmpcursor.row(0); //Â ??
2402 selection.set(true); // to avoid deletion
2404 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2405 selection.cursor = cursor;
2407 setSelection(bview);
2408 // What is this test for ??? (JMarc)
2409 if (!selection.set()) {
2410 deleteWordForward(bview);
2412 cutSelection(bview);
2418 // Change the case of a word at cursor position.
2419 // This function directly manipulates Paragraph::text because there
2420 // is no Paragraph::SetChar currently. I did what I could to ensure
2421 // that it is correct. I guess part of it should be moved to
2422 // Paragraph, but it will have to change for 1.1 anyway. At least
2423 // it does not access outside of the allocated array as the older
2424 // version did. (JMarc)
2425 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2430 if (selection.set()) {
2431 from = selection.start;
2434 getWord(from, to, PARTIAL_WORD);
2435 setCursor(bview, to.par(), to.pos() + 1);
2438 changeRegionCase(bview, from, to, action);
2442 void LyXText::changeRegionCase(BufferView * bview,
2443 LyXCursor const & from,
2444 LyXCursor const & to,
2445 LyXText::TextCase action)
2447 lyx::Assert(from <= to);
2449 setUndo(bview, Undo::FINISH,
2450 from.par(), to.par()->next());
2452 Paragraph::size_type pos = from.pos();
2453 Paragraph * par = from.par();
2455 while (par && (pos != to.pos() || par != to.par())) {
2456 unsigned char c = par->getChar(pos);
2457 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2459 case text_lowercase:
2462 case text_capitalization:
2464 action = text_lowercase;
2466 case text_uppercase:
2471 par->setChar(pos, c);
2472 checkParagraph(bview, par, pos);
2475 if (pos == par->size()) {
2480 if (to.row() != from.row()) {
2481 refresh_y = from.y() - from.row()->baseline();
2482 refresh_row = from.row();
2483 status(bview, LyXText::NEED_MORE_REFRESH);
2488 void LyXText::transposeChars(BufferView & bview)
2490 Paragraph * tmppar = cursor.par();
2492 setUndo(&bview, Undo::FINISH,
2493 tmppar, tmppar->next());
2495 Paragraph::size_type tmppos = cursor.pos();
2497 // First decide if it is possible to transpose at all
2499 // We are at the beginning of a paragraph.
2500 if (tmppos == 0) return;
2502 // We are at the end of a paragraph.
2503 if (tmppos == tmppar->size() - 1) return;
2505 unsigned char c1 = tmppar->getChar(tmppos);
2506 unsigned char c2 = tmppar->getChar(tmppos - 1);
2508 if (c1 != Paragraph::META_INSET
2509 && c2 != Paragraph::META_INSET) {
2510 tmppar->setChar(tmppos, c2);
2511 tmppar->setChar(tmppos - 1, c1);
2513 // We should have an implementation that handles insets
2514 // as well, but that will have to come later. (Lgb)
2515 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2519 void LyXText::Delete(BufferView * bview)
2521 // this is a very easy implementation
2523 LyXCursor old_cursor = cursor;
2524 int const old_cur_par_id = old_cursor.par()->id();
2525 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2526 old_cursor.par()->previous()->id() : 0;
2528 // just move to the right
2531 // CHECK Look at the comment here.
2532 // This check is not very good...
2533 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2534 // and that can very well delete the par or par->previous in
2535 // old_cursor. Will a solution where we compare paragraph id's
2537 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2538 == old_cur_par_prev_id
2539 && cursor.par()->id() != old_cur_par_id)
2540 return; // delete-empty-paragraph-mechanism has done it
2542 // if you had success make a backspace
2543 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2544 LyXCursor tmpcursor = cursor;
2545 cursor = old_cursor; // to make sure undo gets the right cursor position
2546 setUndo(bview, Undo::DELETE,
2547 cursor.par(), cursor.par()->next());
2554 void LyXText::backspace(BufferView * bview)
2556 // Get the font that is used to calculate the baselineskip
2557 Paragraph::size_type lastpos = cursor.par()->size();
2558 LyXFont rawparfont =
2559 cursor.par()->getFontSettings(bview->buffer()->params,
2562 if (cursor.pos() == 0) {
2563 // The cursor is at the beginning of a paragraph,
2564 // so the the backspace will collapse two paragraphs into one.
2566 // we may paste some paragraphs
2568 // is it an empty paragraph?
2571 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2572 // This is an empty paragraph and we delete it just by moving the cursor one step
2573 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2574 // of the paragraph.
2576 if (cursor.par()->previous()) {
2577 Paragraph * tmppar = cursor.par()->previous();
2578 if (cursor.par()->getLayout() == tmppar->getLayout()
2579 && cursor.par()->getAlign() == tmppar->getAlign()) {
2580 // Inherit bottom DTD from the paragraph below.
2581 // (the one we are deleting)
2582 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2583 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2584 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2589 // the layout things can change the height of a row !
2590 int const tmpheight = cursor.row()->height();
2591 setHeightOfRow(bview, cursor.row());
2592 if (cursor.row()->height() != tmpheight) {
2593 refresh_y = cursor.y() - cursor.row()->baseline();
2594 refresh_row = cursor.row();
2595 status(bview, LyXText::NEED_MORE_REFRESH);
2601 if (cursor.par()->previous()) {
2602 setUndo(bview, Undo::DELETE,
2603 cursor.par()->previous(),
2604 cursor.par()->next());
2607 Paragraph * tmppar = cursor.par();
2608 Row * tmprow = cursor.row();
2610 // We used to do cursorLeftIntern() here, but it is
2611 // not a good idea since it triggers the auto-delete
2612 // mechanism. So we do a cursorLeftIntern()-lite,
2613 // without the dreaded mechanism. (JMarc)
2614 if (cursor.par()->previous()) {
2615 // steps into the above paragraph.
2616 setCursorIntern(bview, cursor.par()->previous(),
2617 cursor.par()->previous()->size(),
2621 /* Pasting is not allowed, if the paragraphs have different
2622 layout. I think it is a real bug of all other
2623 word processors to allow it. It confuses the user.
2624 Even so with a footnote paragraph and a non-footnote
2625 paragraph. I will not allow pasting in this case,
2626 because the user would be confused if the footnote behaves
2627 different wether it is open or closed.
2629 Correction: Pasting is always allowed with standard-layout
2631 if (cursor.par() != tmppar
2632 && (cursor.par()->getLayout() == tmppar->getLayout()
2633 || tmppar->getLayout() == 0 /*standard*/)
2634 && cursor.par()->getAlign() == tmppar->getAlign()) {
2636 removeParagraph(tmprow);
2638 cursor.par()->pasteParagraph(bview->buffer()->params);
2640 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2641 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2642 // strangely enough it seems that commenting out the line above removes
2643 // most or all of the segfaults. I will however also try to move the
2644 // two Remove... lines in front of the PasteParagraph too.
2647 cursor.pos(cursor.pos() - 1);
2649 status(bview, LyXText::NEED_MORE_REFRESH);
2650 refresh_row = cursor.row();
2651 refresh_y = cursor.y() - cursor.row()->baseline();
2653 // remove the lost paragraph
2654 // This one is not safe, since the paragraph that the tmprow and the
2655 // following rows belong to has been deleted by the PasteParagraph
2656 // above. The question is... could this be moved in front of the
2658 //RemoveParagraph(tmprow);
2659 //RemoveRow(tmprow);
2661 // This rebuilds the rows.
2662 appendParagraph(bview, cursor.row());
2663 updateCounters(bview, cursor.row());
2665 // the row may have changed, block, hfills etc.
2666 setCursor(bview, cursor.par(), cursor.pos(), false);
2669 /* this is the code for a normal backspace, not pasting
2671 setUndo(bview, Undo::DELETE,
2672 cursor.par(), cursor.par()->next());
2673 // We used to do cursorLeftIntern() here, but it is
2674 // not a good idea since it triggers the auto-delete
2675 // mechanism. So we do a cursorLeftIntern()-lite,
2676 // without the dreaded mechanism. (JMarc)
2677 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2678 false, cursor.boundary());
2680 // some insets are undeletable here
2681 if (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
2682 if (!cursor.par()->getInset(cursor.pos())->deletable())
2684 // force complete redo when erasing display insets
2685 // this is a cruel method but safe..... Matthias
2686 if (cursor.par()->getInset(cursor.pos())->display() ||
2687 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2688 cursor.par()->erase(cursor.pos());
2689 redoParagraph(bview);
2694 Row * row = cursor.row();
2695 int y = cursor.y() - row->baseline();
2696 Paragraph::size_type z;
2697 /* remember that a space at the end of a row doesnt count
2698 * when calculating the fill */
2699 if (cursor.pos() < rowLast(row) ||
2700 !cursor.par()->isLineSeparator(cursor.pos())) {
2701 row->fill(row->fill() + singleWidth(bview,
2706 /* some special code when deleting a newline. This is similar
2707 * to the behavior when pasting paragraphs */
2708 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2709 cursor.par()->erase(cursor.pos());
2710 // refresh the positions
2712 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2713 tmprow = tmprow->next();
2714 tmprow->pos(tmprow->pos() - 1);
2716 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2717 cursor.pos(cursor.pos() - 1);
2719 if (cursor.pos() < cursor.par()->size()
2720 && !cursor.par()->isSeparator(cursor.pos())) {
2721 cursor.par()->insertChar(cursor.pos(), ' ');
2722 setCharFont(bview->buffer(), cursor.par(),
2723 cursor.pos(), current_font);
2724 // refresh the positions
2726 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2727 tmprow = tmprow->next();
2728 tmprow->pos(tmprow->pos() + 1);
2732 cursor.par()->erase(cursor.pos());
2734 // refresh the positions
2736 while (tmprow->next()
2737 && tmprow->next()->par() == row->par()) {
2738 tmprow = tmprow->next();
2739 tmprow->pos(tmprow->pos() - 1);
2742 // delete newlines at the beginning of paragraphs
2743 while (cursor.par()->size() &&
2744 cursor.par()->isNewline(cursor.pos()) &&
2745 cursor.pos() == beginningOfMainBody(bview->buffer(),
2747 cursor.par()->erase(cursor.pos());
2748 // refresh the positions
2750 while (tmprow->next() &&
2751 tmprow->next()->par() == row->par()) {
2752 tmprow = tmprow->next();
2753 tmprow->pos(tmprow->pos() - 1);
2758 // is there a break one row above
2759 if (row->previous() && row->previous()->par() == row->par()) {
2760 z = nextBreakPoint(bview, row->previous(),
2762 if (z >= row->pos()) {
2765 Row * tmprow = row->previous();
2767 // maybe the current row is now empty
2768 if (row->pos() >= row->par()->size()) {
2773 breakAgainOneRow(bview, row);
2774 if (row->next() && row->next()->par() == row->par())
2775 need_break_row = row->next();
2780 // set the dimensions of the row above
2781 y -= tmprow->height();
2782 tmprow->fill(fill(bview, tmprow,
2784 setHeightOfRow(bview, tmprow);
2787 refresh_row = tmprow;
2788 status(bview, LyXText::NEED_MORE_REFRESH);
2789 setCursor(bview, cursor.par(), cursor.pos(),
2790 false, cursor.boundary());
2791 //current_font = rawtmpfont;
2792 //real_current_font = realtmpfont;
2793 // check, whether the last character's font has changed.
2795 cursor.par()->getFontSettings(bview->buffer()->params,
2796 cursor.par()->size() - 1))
2797 redoHeightOfParagraph(bview, cursor);
2802 // break the cursor row again
2803 if (row->next() && row->next()->par() == row->par() &&
2804 (rowLast(row) == row->par()->size() - 1 ||
2805 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2807 /* it can happen that a paragraph loses one row
2808 * without a real breakup. This is when a word
2809 * is to long to be broken. Well, I don t care this
2811 if (rowLast(row) == row->par()->size() - 1)
2812 removeRow(row->next());
2816 status(bview, LyXText::NEED_MORE_REFRESH);
2818 breakAgainOneRow(bview, row);
2819 // will the cursor be in another row now?
2820 if (row->next() && row->next()->par() == row->par() &&
2821 rowLast(row) <= cursor.pos()) {
2823 breakAgainOneRow(bview, row);
2826 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2828 if (row->next() && row->next()->par() == row->par())
2829 need_break_row = row->next();
2833 // set the dimensions of the row
2834 row->fill(fill(bview, row, workWidth(bview)));
2835 int const tmpheight = row->height();
2836 setHeightOfRow(bview, row);
2837 if (tmpheight == row->height())
2838 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2840 status(bview, LyXText::NEED_MORE_REFRESH);
2843 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2847 // current_font = rawtmpfont;
2848 // real_current_font = realtmpfont;
2850 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2851 != cursor.boundary())
2852 setCursor(bview, cursor.par(), cursor.pos(), false,
2853 !cursor.boundary());
2855 lastpos = cursor.par()->size();
2856 if (cursor.pos() == lastpos)
2857 setCurrentFont(bview);
2859 // check, whether the last characters font has changed.
2861 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2862 redoHeightOfParagraph(bview, cursor);
2864 // now the special right address boxes
2865 if (textclasslist.Style(bview->buffer()->params.textclass,
2866 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2867 redoDrawingOfParagraph(bview, cursor);
2873 void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
2874 Row * row_ptr, int y, bool cleared)
2876 // returns a printed row
2877 Painter & pain = bview->painter();
2880 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
2882 Paragraph::size_type const last = rowLastPrintable(row_ptr);
2884 Paragraph::size_type vpos;
2885 Paragraph::size_type pos;
2889 LyXFont font(LyXFont::ALL_SANE);
2891 if (row_ptr->height() <= 0) {
2892 lyxerr << "LYX_ERROR: row.height: "
2893 << row_ptr->height() << endl;
2898 float fill_separator;
2900 float fill_label_hfill;
2901 prepareToPrint(bview, row_ptr, x, fill_separator,
2902 fill_hfill, fill_label_hfill);
2904 if (inset_owner && (x < 0))
2908 // clear the area where we want to paint/print
2909 int const ww = bview->workWidth();
2911 bool clear_area = true;
2914 if (!bview->screen()->forceClear() && last == row_ptr->pos()
2915 && row_ptr->par()->getChar(row_ptr->pos()) == Paragraph::META_INSET
2916 && (inset = row_ptr->par()->getInset(row_ptr->pos()))) {
2917 clear_area = inset->doClearArea();
2919 // we don't need to clear it's already done!!!
2922 } else if (clear_area) {
2923 #ifdef WITH_WARNINGS
2924 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
2925 #warning Should we not fix this in the Painter, please have a look Lars! (Jug)
2927 int const y = y_offset < 0 ? 0 : y_offset;
2928 int const h = y_offset < 0 ?
2929 row_ptr->height() + y_offset : row_ptr->height();
2930 int const w = inset_owner ?
2931 inset_owner->width(bview, font) - 2 : ww;
2932 int const x = x_offset;
2933 pain.fillRectangle(x, y, w, h);
2934 } else if (inset != 0) {
2935 int h = row_ptr->baseline() - inset->ascent(bview, font);
2937 int const w = (inset_owner ?
2938 inset_owner->width(bview, font) : ww);
2939 pain.fillRectangle(x_offset, y_offset, w, h);
2941 h += inset->ascent(bview, font) + inset->descent(bview, font);
2942 if ((row_ptr->height() - h) > 0) {
2943 int const w = (inset_owner ?
2944 inset_owner->width(bview, font) : ww);
2945 pain.fillRectangle(x_offset, y_offset + h,
2946 w, row_ptr->height() - h);
2948 if (!inset_owner && !inset->display() && !inset->needFullRow())
2950 int const w = inset->width(bview, font) + int(x);
2951 pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
2955 if (selection.set()) {
2956 int const w = (inset_owner ?
2957 inset_owner->width(bview, font) : ww);
2959 if (bidi_same_direction) {
2960 if (selection.start.row() == row_ptr &&
2961 selection.end.row() == row_ptr) {
2962 if (selection.start.x() < selection.end.x())
2963 pain.fillRectangle(x_offset + selection.start.x(),
2965 selection.end.x() - selection.start.x(),
2969 pain.fillRectangle(x_offset + selection.end.x(),
2971 selection.start.x() - selection.end.x(),
2974 } else if (selection.start.row() == row_ptr) {
2976 pain.fillRectangle(x_offset, y_offset,
2977 selection.start.x(),
2981 pain.fillRectangle(x_offset + selection.start.x(),
2983 w - selection.start.x(),
2986 } else if (selection.end.row() == row_ptr) {
2988 pain.fillRectangle(x_offset + selection.end.x(),
2990 w - selection.end.x(),
2994 pain.fillRectangle(x_offset, y_offset,
2998 } else if (y > selection.start.y()
2999 && y < selection.end.y()) {
3000 pain.fillRectangle(x_offset, y_offset, w,
3004 } else if (selection.start.row() != row_ptr &&
3005 selection.end.row() != row_ptr &&
3006 y > selection.start.y()
3007 && y < selection.end.y()) {
3008 pain.fillRectangle(x_offset, y_offset, w,
3011 } else if (selection.start.row() == row_ptr ||
3012 selection.end.row() == row_ptr) {
3014 if ((selection.start.row() != row_ptr && !is_rtl) ||
3015 (selection.end.row() != row_ptr && is_rtl))
3016 pain.fillRectangle(x_offset, y_offset,
3020 Paragraph::size_type main_body =
3021 beginningOfMainBody(bview->buffer(),
3024 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3025 pos = vis2log(vpos);
3026 float const old_tmpx = tmpx;
3027 if (main_body > 0 && pos == main_body-1) {
3028 tmpx += fill_label_hfill +
3029 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3030 row_ptr->par()->getLayout()).labelsep,
3031 getFont(bview->buffer(),row_ptr->par(), -2));
3032 if (row_ptr->par()->isLineSeparator(main_body-1))
3033 tmpx -= singleWidth(bview, row_ptr->par(), main_body-1);
3035 if (hfillExpansion(bview->buffer(), row_ptr, pos)) {
3036 tmpx += singleWidth(bview, row_ptr->par(), pos);
3037 if (pos >= main_body)
3040 tmpx += fill_label_hfill;
3042 else if (row_ptr->par()->isSeparator(pos)) {
3043 tmpx += singleWidth(bview, row_ptr->par(), pos);
3044 if (pos >= main_body)
3045 tmpx += fill_separator;
3047 tmpx += singleWidth(bview, row_ptr->par(), pos);
3049 if ((selection.start.row() != row_ptr ||
3050 selection.start.pos() <= pos) &&
3051 (selection.end.row() != row_ptr ||
3052 pos < selection.end.pos()) )
3053 // Here we do not use x_offset as x_offset was
3055 pain.fillRectangle(int(old_tmpx),
3057 int(tmpx - old_tmpx + 1),
3062 if ((selection.start.row() != row_ptr && is_rtl) ||
3063 (selection.end.row() != row_ptr && !is_rtl) )
3064 pain.fillRectangle(x_offset + int(tmpx),
3074 // Draw appendix lines
3075 Paragraph * firstpar = row_ptr->par();
3077 if (firstpar->params().appendix()) {
3078 pain.line(1, y_offset,
3079 1, y_offset + row_ptr->height(),
3080 LColor::appendixline);
3081 pain.line(ww - 2, y_offset,
3082 ww - 2, y_offset + row_ptr->height(),
3083 LColor::appendixline);
3087 Paragraph::depth_type const depth = firstpar->getDepth();
3089 Paragraph::depth_type next_depth = 0;
3090 Paragraph::depth_type prev_depth = 0;
3091 if (row_ptr->next())
3092 next_depth = row_ptr->next()->par()->getDepth();
3093 if (row_ptr->previous())
3094 prev_depth = row_ptr->previous()->par()->getDepth();
3096 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3097 int const line_x = (LYX_PAPER_MARGIN / 5) *
3098 i + box_x + x_offset;
3099 pain.line(line_x, y_offset, line_x,
3100 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3104 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3107 pain.fillRectangle(line_x,
3108 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3109 LYX_PAPER_MARGIN / 5, 2,
3115 LyXLayout const & layout =
3116 textclasslist.Style(bview->buffer()->params.textclass,
3117 row_ptr->par()->getLayout());
3120 int y_bottom = row_ptr->height();
3122 // is it a first row?
3123 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3125 // start of appendix?
3126 if (row_ptr->par()->params().startOfAppendix()) {
3127 pain.line(1, y_offset,
3129 LColor::appendixline);
3132 // think about the margins
3133 if (!row_ptr->previous() && bv_owner)
3134 y_top += LYX_PAPER_MARGIN;
3136 // draw a top pagebreak
3137 if (row_ptr->par()->params().pagebreakTop()) {
3139 pb_font.setColor(LColor::pagebreak).decSize();
3143 pain.line(0, y_offset + y_top + 2*defaultHeight(),
3145 y_offset + y_top + 2 * defaultHeight(),
3147 Painter::line_onoffdash)
3150 _("Page Break (top)"),
3153 LColor::background, false, w, a, d);
3154 pain.rectText((ww - w)/2,
3155 y_offset + y_top + 2 * defaultHeight() + d,
3156 _("Page Break (top)"),
3159 LColor::background);
3160 y_top += 3 * defaultHeight();
3163 if (row_ptr->par()->params().spaceTop().kind() == VSpace::VFILL) {
3165 pain.line(0, y_offset + 2 + y_top,
3166 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3169 pain.line(0, y_offset + y_top + 3 * defaultHeight(),
3171 y_offset + y_top + 3 * defaultHeight(),
3174 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3175 LYX_PAPER_MARGIN / 2,
3176 y_offset + y_top + 3 * defaultHeight(),
3179 y_top += 3 * defaultHeight();
3182 // think about user added space
3183 y_top += int(row_ptr->par()->params().spaceTop().inPixels(bview));
3185 // think about the parskip
3186 // some parskips VERY EASY IMPLEMENTATION
3187 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3188 if (layout.latextype == LATEX_PARAGRAPH
3189 && firstpar->getDepth() == 0
3190 && firstpar->previous())
3191 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3192 else if (firstpar->previous()
3193 && textclasslist.Style(bview->buffer()->params.textclass,
3194 firstpar->previous()->getLayout()).latextype == LATEX_PARAGRAPH
3195 && firstpar->previous()->getDepth() == 0)
3196 // is it right to use defskip here, too? (AS)
3197 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3200 if (row_ptr->par()->params().lineTop()) {
3202 y_top += lyxfont::ascent('x',
3203 getFont(bview->buffer(),
3204 row_ptr->par(), 0));
3205 int const w = (inset_owner ?
3206 inset_owner->width(bview, font) : ww);
3207 int const xp = static_cast<int>(inset_owner ? x : 0);
3208 pain.line(xp, y_offset + y_top,
3209 w, y_offset + y_top,
3211 Painter::line_solid,
3212 Painter::line_thick);
3214 y_top += lyxfont::ascent('x',getFont(bview->buffer(),
3215 row_ptr->par(), 0));
3218 // should we print a label?
3219 if (layout.labeltype >= LABEL_STATIC
3220 && (layout.labeltype != LABEL_STATIC
3221 || layout.latextype != LATEX_ENVIRONMENT
3222 || row_ptr->par()->isFirstInSequence())) {
3223 font = getFont(bview->buffer(), row_ptr->par(), -2);
3224 if (!row_ptr->par()->getLabelstring().empty()) {
3226 string const tmpstring =
3227 row_ptr->par()->getLabelstring();
3229 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3230 if (bview->buffer()->params.secnumdepth >= 0) {
3231 // this is special code for
3232 // the chapter layout. This is
3233 // printed in an extra row
3234 // and has a pagebreak at
3236 float spacing_val = 1.0;
3237 if (!row_ptr->par()->params().spacing().isDefault()) {
3238 spacing_val = row_ptr->par()->params().spacing().getValue();
3240 spacing_val = bview->buffer()->params.spacing.getValue();
3243 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3244 + int(layout.parsep) * defaultHeight();
3246 tmpx = ww - leftMargin(bview, row_ptr) -
3247 lyxfont::width(tmpstring, font);
3248 pain.text(int(tmpx),
3249 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3254 tmpx = ww - leftMargin(bview, row_ptr)
3255 + lyxfont::width(layout.labelsep, font);
3257 tmpx = x - lyxfont::width(layout.labelsep, font)
3258 - lyxfont::width(tmpstring, font);
3261 pain.text(int(tmpx),
3262 y_offset + row_ptr->baseline(),
3266 // the labels at the top of an environment.
3267 // More or less for bibliography
3268 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3269 layout.labeltype == LABEL_BIBLIO ||
3270 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3271 if (row_ptr->par()->isFirstInSequence()) {
3272 font = getFont(bview->buffer(),
3273 row_ptr->par(), -2);
3274 if (!row_ptr->par()->getLabelstring().empty()) {
3275 string const tmpstring =
3276 row_ptr->par()->getLabelstring();
3277 float spacing_val = 1.0;
3278 if (!row_ptr->par()->params().spacing().isDefault()) {
3279 spacing_val = row_ptr->par()->params().spacing().getValue();
3281 spacing_val = bview->buffer()->params.spacing.getValue();
3284 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3285 + (layout.labelbottomsep * defaultHeight()));
3288 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3289 tmpx = ( (is_rtl ? leftMargin(bview, row_ptr) : x)
3290 + ww - rightMargin(bview->buffer(), row_ptr) ) / 2;
3291 tmpx -= lyxfont::width(tmpstring, font) / 2;
3293 tmpx = ww - leftMargin(bview, row_ptr) -
3294 lyxfont::width(tmpstring, font);
3295 pain.text(int(tmpx),
3296 y_offset + row_ptr->baseline()
3297 - row_ptr->ascent_of_text()
3303 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3304 font = getFont(bview->buffer(), row_ptr->par(), -1);
3306 tmpx = ww - leftMargin(bview, row_ptr)
3307 + lyxfont::width(layout.labelsep, font);
3309 tmpx = x - lyxfont::width(layout.labelsep, font)
3310 - row_ptr->par()->bibkey->width(bview, font);
3311 row_ptr->par()->bibkey->draw(bview, font,
3312 y_offset + row_ptr->baseline(),
3317 // is it a last row?
3318 Paragraph * par = row_ptr->par();
3319 if (row_ptr->par() == par
3320 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
3321 // think about the margins
3322 if (!row_ptr->next() && bv_owner)
3323 y_bottom -= LYX_PAPER_MARGIN;
3325 // draw a bottom pagebreak
3326 if (firstpar->params().pagebreakBottom()) {
3328 pb_font.setColor(LColor::pagebreak).decSize();
3329 int const y_place = y_offset + y_bottom
3330 - 2 * defaultHeight();
3336 .line(0, y_place, ww, y_place,
3338 Painter::line_onoffdash)
3340 _("Page Break (bottom)"),
3343 LColor::background, false, w, a, d);
3344 pain.rectText((ww - w) / 2, y_place + d,
3345 _("Page Break (bottom)"),
3348 LColor::background);
3349 y_bottom -= 3 * defaultHeight();
3352 if (firstpar->params().spaceBottom().kind() == VSpace::VFILL) {
3353 // draw a vfill bottom
3354 int const y_place = y_offset + y_bottom
3355 - 3 * defaultHeight();
3357 pain.line(0, y_place,
3358 LYX_PAPER_MARGIN, y_place,
3360 pain.line(0, y_offset + y_bottom - 2,
3362 y_offset + y_bottom - 2,
3364 pain.line(LYX_PAPER_MARGIN / 2,
3366 LYX_PAPER_MARGIN / 2,
3367 y_offset + y_bottom - 2,
3369 y_bottom -= 3 * defaultHeight();
3372 // think about user added space
3373 y_bottom -= int(firstpar->params().spaceBottom().inPixels(bview));
3375 if (firstpar->params().lineBottom()) {
3376 // draw a bottom line
3377 y_bottom -= lyxfont::ascent('x',
3378 getFont(bview->buffer(),
3381 int const w = (inset_owner ?
3382 inset_owner->width(bview, font) : ww);
3383 int const xp = static_cast<int>(inset_owner ? x : 0);
3384 pain.line(xp, y_offset + y_bottom,
3385 w, y_offset + y_bottom,
3386 LColor::topline, Painter::line_solid,
3387 Painter::line_thick);
3388 y_bottom -= lyxfont::ascent('x',
3389 getFont(bview->buffer(),
3395 int const endlabel =
3396 row_ptr->par()->getEndLabel(bview->buffer()->params);
3399 case END_LABEL_FILLED_BOX:
3401 LyXFont const font = getFont(bview->buffer(),
3402 row_ptr->par(), last);
3403 int const size = int(0.75 * lyxfont::maxAscent(font));
3404 int const y = (y_offset + row_ptr->baseline()) - size;
3405 int x = is_rtl ? LYX_PAPER_MARGIN
3406 : ww - LYX_PAPER_MARGIN - size;
3408 if (row_ptr->fill() <= size)
3409 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3410 if (endlabel == END_LABEL_BOX) {
3411 pain.line(x, y, x, y + size,
3413 pain.line(x + size, y, x + size , y + size,
3415 pain.line(x, y, x + size, y,
3417 pain.line(x, y + size, x + size, y + size,
3420 pain.fillRectangle(x, y, size, size,
3424 case END_LABEL_STATIC:
3426 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->getLayout();
3427 string const tmpstring = textclasslist.
3428 Style(bview->buffer()->params.textclass,
3429 layout).endlabelstring();
3430 font = getFont(bview->buffer(), row_ptr->par(), -2);
3431 int const tmpx = is_rtl ?
3432 int(x) - lyxfont::width(tmpstring, font)
3433 : ww - rightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
3434 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
3437 case END_LABEL_NO_LABEL:
3442 // draw the text in the pixmap
3444 vpos = row_ptr->pos();
3446 Paragraph::size_type main_body =
3447 beginningOfMainBody(bview->buffer(), row_ptr->par());
3448 if (main_body > 0 &&
3449 (main_body-1 > last ||
3450 !row_ptr->par()->isLineSeparator(main_body - 1)))
3453 while (vpos <= last) {
3454 pos = vis2log(vpos);
3455 if (main_body > 0 && pos == main_body - 1) {
3456 x += fill_label_hfill
3457 + lyxfont::width(layout.labelsep,
3458 getFont(bview->buffer(),
3459 row_ptr->par(), -2))
3460 - singleWidth(bview,
3465 if (row_ptr->par() ->isHfill(pos)) {
3468 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3470 y_offset + row_ptr->baseline(),
3473 if (hfillExpansion(bview->buffer(),
3475 if (pos >= main_body) {
3477 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3478 int(x + fill_hfill),
3479 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3481 Painter::line_onoffdash);
3485 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3486 int(x + fill_label_hfill),
3487 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3489 Painter::line_onoffdash);
3491 x += fill_label_hfill;
3494 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3496 y_offset + row_ptr->baseline(),
3501 } else if (row_ptr->par()->isSeparator(pos)) {
3502 x += singleWidth(bview,
3503 row_ptr->par(), pos);
3504 if (pos >= main_body)
3505 x += fill_separator;
3508 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
3513 int LyXText::defaultHeight() const
3515 LyXFont font(LyXFont::ALL_SANE);
3516 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3520 /* returns the column near the specified x-coordinate of the row
3521 * x is set to the real beginning of this column */
3522 int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3523 bool & boundary) const
3526 float fill_separator;
3528 float fill_label_hfill;
3530 prepareToPrint(bview, row, tmpx, fill_separator,
3531 fill_hfill, fill_label_hfill);
3533 Paragraph::size_type vc = row->pos();
3534 Paragraph::size_type last = rowLastPrintable(row);
3535 Paragraph::size_type c = 0;
3536 LyXLayout const & layout =
3537 textclasslist.Style(bview->buffer()->params.textclass,
3538 row->par()->getLayout());
3539 bool left_side = false;
3541 Paragraph::size_type
3542 main_body = beginningOfMainBody(bview->buffer(), row->par());
3543 float last_tmpx = tmpx;
3545 if (main_body > 0 &&
3546 (main_body - 1 > last ||
3547 !row->par()->isLineSeparator(main_body - 1)))
3550 while (vc <= last && tmpx <= x) {
3553 if (main_body > 0 && c == main_body-1) {
3554 tmpx += fill_label_hfill +
3555 lyxfont::width(layout.labelsep,
3556 getFont(bview->buffer(), row->par(), -2));
3557 if (row->par()->isLineSeparator(main_body - 1))
3558 tmpx -= singleWidth(bview, row->par(), main_body-1);
3561 if (hfillExpansion(bview->buffer(), row, c)) {
3562 x += singleWidth(bview, row->par(), c);
3566 tmpx += fill_label_hfill;
3568 else if (row->par()->isSeparator(c)) {
3569 tmpx += singleWidth(bview, row->par(), c);
3571 tmpx+= fill_separator;
3573 tmpx += singleWidth(bview, row->par(), c);
3577 if ((tmpx + last_tmpx) / 2 > x) {
3582 if (vc > last + 1) // This shouldn't happen.
3586 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3587 // some speedup if rtl_support=false
3588 && (!row->next() || row->next()->par() != row->par());
3589 bool const rtl = (lastrow)
3590 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3591 : false; // If lastrow is false, we don't need to compute
3592 // the value of rtl.
3594 if (row->pos() > last) // Row is empty?
3597 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3598 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3600 else if (vc == row->pos()) {
3602 if (bidi_level(c) % 2 == 1)
3605 c = vis2log(vc - 1);
3606 bool const rtl = (bidi_level(c) % 2 == 1);
3607 if (left_side == rtl) {
3609 boundary = isBoundary(bview->buffer(), row->par(), c);
3613 if (row->pos() <= last && c > last
3614 && row->par()->isNewline(last)) {
3615 if (bidi_level(last) % 2 == 0)
3616 tmpx -= singleWidth(bview, row->par(), last);
3618 tmpx += singleWidth(bview, row->par(), last);
3628 // returns pointer to a specified row
3629 Row * LyXText::getRow(Paragraph * par,
3630 Paragraph::size_type pos, int & y) const
3635 Row * tmprow = firstrow;
3638 // find the first row of the specified paragraph
3639 while (tmprow->next() && tmprow->par() != par) {
3640 y += tmprow->height();
3641 tmprow = tmprow->next();
3644 // now find the wanted row
3645 while (tmprow->pos() < pos
3647 && tmprow->next()->par() == par
3648 && tmprow->next()->pos() <= pos) {
3649 y += tmprow->height();
3650 tmprow = tmprow->next();