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 Paragraph * tmppar = cursor.par();
2229 // If this is not the very first word, skip rest of
2230 // current word because we are probably in the middle
2231 // of a word if there is text here.
2232 if (cursor.pos() || cursor.par()->previous()) {
2233 while (cursor.pos() < cursor.par()->size()
2234 && cursor.par()->isLetter(cursor.pos()))
2235 cursor.pos(cursor.pos() + 1);
2238 // Now, skip until we have real text (will jump paragraphs)
2239 while ((cursor.par()->size() > cursor.pos()
2240 && (!cursor.par()->isLetter(cursor.pos())
2242 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2243 .latex() == LyXFont::ON
2246 || (cursor.par()->size() == cursor.pos()
2247 && cursor.par()->next())){
2248 if (cursor.pos() == cursor.par()->size()) {
2249 cursor.par(cursor.par()->next());
2252 cursor.pos(cursor.pos() + 1);
2255 // Update the value if we changed paragraphs
2256 if (cursor.par() != tmppar){
2257 setCursor(bview, cursor.par(), cursor.pos());
2258 value = float(cursor.y())/float(height);
2261 // Start the selection from here
2262 selection.cursor = cursor;
2264 std::ostringstream latex;
2266 // and find the end of the word
2267 // (optional hyphens are part of a word)
2268 while (cursor.pos() < cursor.par()->size()
2269 && (cursor.par()->isLetter(cursor.pos()))
2270 || (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET
2271 && cursor.par()->getInset(cursor.pos()) != 0
2272 && cursor.par()->getInset(cursor.pos())->latex(bview->buffer(), latex, false, false) == 0
2273 && latex.str() == "\\-"
2275 cursor.pos(cursor.pos() + 1);
2277 // Finally, we copy the word to a string and return it
2279 if (selection.cursor.pos() < cursor.pos()) {
2280 Paragraph::size_type i;
2281 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2282 if (cursor.par()->getChar(i) != Paragraph::META_INSET)
2283 str += cursor.par()->getChar(i);
2290 // This one is also only for the spellchecker
2291 void LyXText::selectSelectedWord(BufferView * bview)
2293 // move cursor to the beginning
2294 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2296 // set the sel cursor
2297 selection.cursor = cursor;
2298 std::ostringstream latex;
2300 // now find the end of the word
2301 while (cursor.pos() < cursor.par()->size()
2302 && (cursor.par()->isLetter(cursor.pos())
2303 || (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET
2304 && cursor.par()->getInset(cursor.pos()) != 0
2305 && cursor.par()->getInset(cursor.pos())->latex(bview->buffer(), latex, false, false) == 0
2306 && latex.str() == "\\-"
2308 cursor.pos(cursor.pos() + 1);
2310 setCursor(bview, cursor.par(), cursor.pos());
2312 // finally set the selection
2313 setSelection(bview);
2317 /* -------> Delete from cursor up to the end of the current or next word. */
2318 void LyXText::deleteWordForward(BufferView * bview)
2320 if (!cursor.par()->size())
2323 LyXCursor tmpcursor = cursor;
2324 tmpcursor.row(0); //Â ??
2325 selection.set(true); // to avoid deletion
2326 cursorRightOneWord(bview);
2327 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2328 selection.cursor = cursor;
2330 setSelection(bview);
2332 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2333 cutSelection(bview);
2338 /* -------> Delete from cursor to start of current or prior word. */
2339 void LyXText::deleteWordBackward(BufferView * bview)
2341 if (!cursor.par()->size())
2344 LyXCursor tmpcursor = cursor;
2345 tmpcursor.row(0); // ??
2346 selection.set(true); // to avoid deletion
2347 cursorLeftOneWord(bview);
2348 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2349 selection.cursor = cursor;
2351 setSelection(bview);
2352 cutSelection(bview);
2357 /* -------> Kill to end of line. */
2358 void LyXText::deleteLineForward(BufferView * bview)
2360 if (!cursor.par()->size())
2361 // Paragraph is empty, so we just go to the right
2364 LyXCursor tmpcursor = cursor;
2365 // We can't store the row over a regular setCursor
2366 // so we set it to 0 and reset it afterwards.
2367 tmpcursor.row(0); //Â ??
2368 selection.set(true); // to avoid deletion
2370 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2371 selection.cursor = cursor;
2373 setSelection(bview);
2374 // What is this test for ??? (JMarc)
2375 if (!selection.set()) {
2376 deleteWordForward(bview);
2378 cutSelection(bview);
2384 // Change the case of a word at cursor position.
2385 // This function directly manipulates Paragraph::text because there
2386 // is no Paragraph::SetChar currently. I did what I could to ensure
2387 // that it is correct. I guess part of it should be moved to
2388 // Paragraph, but it will have to change for 1.1 anyway. At least
2389 // it does not access outside of the allocated array as the older
2390 // version did. (JMarc)
2391 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2396 if (selection.set()) {
2397 from = selection.start;
2400 getWord(from, to, PARTIAL_WORD);
2401 setCursor(bview, to.par(), to.pos() + 1);
2404 changeRegionCase(bview, from, to, action);
2408 void LyXText::changeRegionCase(BufferView * bview,
2409 LyXCursor const & from,
2410 LyXCursor const & to,
2411 LyXText::TextCase action)
2413 lyx::Assert(from <= to);
2415 setUndo(bview, Undo::FINISH,
2416 from.par(), to.par()->next());
2418 Paragraph::size_type pos = from.pos();
2419 Paragraph * par = from.par();
2421 while (par && (pos != to.pos() || par != to.par())) {
2422 unsigned char c = par->getChar(pos);
2423 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2425 case text_lowercase:
2428 case text_capitalization:
2430 action = text_lowercase;
2432 case text_uppercase:
2437 par->setChar(pos, c);
2438 checkParagraph(bview, par, pos);
2441 if (pos == par->size()) {
2446 if (to.row() != from.row()) {
2447 refresh_y = from.y() - from.row()->baseline();
2448 refresh_row = from.row();
2449 status(bview, LyXText::NEED_MORE_REFRESH);
2454 void LyXText::transposeChars(BufferView & bview)
2456 Paragraph * tmppar = cursor.par();
2458 setUndo(&bview, Undo::FINISH,
2459 tmppar, tmppar->next());
2461 Paragraph::size_type tmppos = cursor.pos();
2463 // First decide if it is possible to transpose at all
2465 // We are at the beginning of a paragraph.
2466 if (tmppos == 0) return;
2468 // We are at the end of a paragraph.
2469 if (tmppos == tmppar->size() - 1) return;
2471 unsigned char c1 = tmppar->getChar(tmppos);
2472 unsigned char c2 = tmppar->getChar(tmppos - 1);
2474 if (c1 != Paragraph::META_INSET
2475 && c2 != Paragraph::META_INSET) {
2476 tmppar->setChar(tmppos, c2);
2477 tmppar->setChar(tmppos - 1, c1);
2479 // We should have an implementation that handles insets
2480 // as well, but that will have to come later. (Lgb)
2481 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2485 void LyXText::Delete(BufferView * bview)
2487 // this is a very easy implementation
2489 LyXCursor old_cursor = cursor;
2490 int const old_cur_par_id = old_cursor.par()->id();
2491 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2492 old_cursor.par()->previous()->id() : 0;
2494 // just move to the right
2497 // CHECK Look at the comment here.
2498 // This check is not very good...
2499 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2500 // and that can very well delete the par or par->previous in
2501 // old_cursor. Will a solution where we compare paragraph id's
2503 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2504 == old_cur_par_prev_id
2505 && cursor.par()->id() != old_cur_par_id)
2506 return; // delete-empty-paragraph-mechanism has done it
2508 // if you had success make a backspace
2509 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2510 LyXCursor tmpcursor = cursor;
2511 cursor = old_cursor; // to make sure undo gets the right cursor position
2512 setUndo(bview, Undo::DELETE,
2513 cursor.par(), cursor.par()->next());
2520 void LyXText::backspace(BufferView * bview)
2522 // Get the font that is used to calculate the baselineskip
2523 Paragraph::size_type lastpos = cursor.par()->size();
2524 LyXFont rawparfont =
2525 cursor.par()->getFontSettings(bview->buffer()->params,
2528 if (cursor.pos() == 0) {
2529 // The cursor is at the beginning of a paragraph,
2530 // so the the backspace will collapse two paragraphs into one.
2532 // we may paste some paragraphs
2534 // is it an empty paragraph?
2537 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2538 // This is an empty paragraph and we delete it just by moving the cursor one step
2539 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2540 // of the paragraph.
2542 if (cursor.par()->previous()) {
2543 Paragraph * tmppar = cursor.par()->previous();
2544 if (cursor.par()->getLayout() == tmppar->getLayout()
2545 && cursor.par()->getAlign() == tmppar->getAlign()) {
2546 // Inherit bottom DTD from the paragraph below.
2547 // (the one we are deleting)
2548 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2549 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2550 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2555 // the layout things can change the height of a row !
2556 int const tmpheight = cursor.row()->height();
2557 setHeightOfRow(bview, cursor.row());
2558 if (cursor.row()->height() != tmpheight) {
2559 refresh_y = cursor.y() - cursor.row()->baseline();
2560 refresh_row = cursor.row();
2561 status(bview, LyXText::NEED_MORE_REFRESH);
2567 if (cursor.par()->previous()) {
2568 setUndo(bview, Undo::DELETE,
2569 cursor.par()->previous(),
2570 cursor.par()->next());
2573 Paragraph * tmppar = cursor.par();
2574 Row * tmprow = cursor.row();
2576 // We used to do cursorLeftIntern() here, but it is
2577 // not a good idea since it triggers the auto-delete
2578 // mechanism. So we do a cursorLeftIntern()-lite,
2579 // without the dreaded mechanism. (JMarc)
2580 if (cursor.par()->previous()) {
2581 // steps into the above paragraph.
2582 setCursorIntern(bview, cursor.par()->previous(),
2583 cursor.par()->previous()->size(),
2587 /* Pasting is not allowed, if the paragraphs have different
2588 layout. I think it is a real bug of all other
2589 word processors to allow it. It confuses the user.
2590 Even so with a footnote paragraph and a non-footnote
2591 paragraph. I will not allow pasting in this case,
2592 because the user would be confused if the footnote behaves
2593 different wether it is open or closed.
2595 Correction: Pasting is always allowed with standard-layout
2597 if (cursor.par() != tmppar
2598 && (cursor.par()->getLayout() == tmppar->getLayout()
2599 || tmppar->getLayout() == 0 /*standard*/)
2600 && cursor.par()->getAlign() == tmppar->getAlign()) {
2602 removeParagraph(tmprow);
2604 cursor.par()->pasteParagraph(bview->buffer()->params);
2606 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2607 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2608 // strangely enough it seems that commenting out the line above removes
2609 // most or all of the segfaults. I will however also try to move the
2610 // two Remove... lines in front of the PasteParagraph too.
2613 cursor.pos(cursor.pos() - 1);
2615 status(bview, LyXText::NEED_MORE_REFRESH);
2616 refresh_row = cursor.row();
2617 refresh_y = cursor.y() - cursor.row()->baseline();
2619 // remove the lost paragraph
2620 // This one is not safe, since the paragraph that the tmprow and the
2621 // following rows belong to has been deleted by the PasteParagraph
2622 // above. The question is... could this be moved in front of the
2624 //RemoveParagraph(tmprow);
2625 //RemoveRow(tmprow);
2627 // This rebuilds the rows.
2628 appendParagraph(bview, cursor.row());
2629 updateCounters(bview, cursor.row());
2631 // the row may have changed, block, hfills etc.
2632 setCursor(bview, cursor.par(), cursor.pos(), false);
2635 /* this is the code for a normal backspace, not pasting
2637 setUndo(bview, Undo::DELETE,
2638 cursor.par(), cursor.par()->next());
2639 // We used to do cursorLeftIntern() here, but it is
2640 // not a good idea since it triggers the auto-delete
2641 // mechanism. So we do a cursorLeftIntern()-lite,
2642 // without the dreaded mechanism. (JMarc)
2643 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2644 false, cursor.boundary());
2646 // some insets are undeletable here
2647 if (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
2648 if (!cursor.par()->getInset(cursor.pos())->deletable())
2650 // force complete redo when erasing display insets
2651 // this is a cruel method but safe..... Matthias
2652 if (cursor.par()->getInset(cursor.pos())->display() ||
2653 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2654 cursor.par()->erase(cursor.pos());
2655 redoParagraph(bview);
2660 Row * row = cursor.row();
2661 int y = cursor.y() - row->baseline();
2662 Paragraph::size_type z;
2663 /* remember that a space at the end of a row doesnt count
2664 * when calculating the fill */
2665 if (cursor.pos() < rowLast(row) ||
2666 !cursor.par()->isLineSeparator(cursor.pos())) {
2667 row->fill(row->fill() + singleWidth(bview,
2672 /* some special code when deleting a newline. This is similar
2673 * to the behavior when pasting paragraphs */
2674 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2675 cursor.par()->erase(cursor.pos());
2676 // refresh the positions
2678 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2679 tmprow = tmprow->next();
2680 tmprow->pos(tmprow->pos() - 1);
2682 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2683 cursor.pos(cursor.pos() - 1);
2685 if (cursor.pos() < cursor.par()->size()
2686 && !cursor.par()->isSeparator(cursor.pos())) {
2687 cursor.par()->insertChar(cursor.pos(), ' ');
2688 setCharFont(bview->buffer(), cursor.par(),
2689 cursor.pos(), current_font);
2690 // refresh the positions
2692 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2693 tmprow = tmprow->next();
2694 tmprow->pos(tmprow->pos() + 1);
2698 cursor.par()->erase(cursor.pos());
2700 // refresh the positions
2702 while (tmprow->next()
2703 && tmprow->next()->par() == row->par()) {
2704 tmprow = tmprow->next();
2705 tmprow->pos(tmprow->pos() - 1);
2708 // delete newlines at the beginning of paragraphs
2709 while (cursor.par()->size() &&
2710 cursor.par()->isNewline(cursor.pos()) &&
2711 cursor.pos() == beginningOfMainBody(bview->buffer(),
2713 cursor.par()->erase(cursor.pos());
2714 // refresh the positions
2716 while (tmprow->next() &&
2717 tmprow->next()->par() == row->par()) {
2718 tmprow = tmprow->next();
2719 tmprow->pos(tmprow->pos() - 1);
2724 // is there a break one row above
2725 if (row->previous() && row->previous()->par() == row->par()) {
2726 z = nextBreakPoint(bview, row->previous(),
2728 if (z >= row->pos()) {
2731 Row * tmprow = row->previous();
2733 // maybe the current row is now empty
2734 if (row->pos() >= row->par()->size()) {
2739 breakAgainOneRow(bview, row);
2740 if (row->next() && row->next()->par() == row->par())
2741 need_break_row = row->next();
2746 // set the dimensions of the row above
2747 y -= tmprow->height();
2748 tmprow->fill(fill(bview, tmprow,
2750 setHeightOfRow(bview, tmprow);
2753 refresh_row = tmprow;
2754 status(bview, LyXText::NEED_MORE_REFRESH);
2755 setCursor(bview, cursor.par(), cursor.pos(),
2756 false, cursor.boundary());
2757 //current_font = rawtmpfont;
2758 //real_current_font = realtmpfont;
2759 // check, whether the last character's font has changed.
2761 cursor.par()->getFontSettings(bview->buffer()->params,
2762 cursor.par()->size() - 1))
2763 redoHeightOfParagraph(bview, cursor);
2768 // break the cursor row again
2769 if (row->next() && row->next()->par() == row->par() &&
2770 (rowLast(row) == row->par()->size() - 1 ||
2771 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2773 /* it can happen that a paragraph loses one row
2774 * without a real breakup. This is when a word
2775 * is to long to be broken. Well, I don t care this
2777 if (rowLast(row) == row->par()->size() - 1)
2778 removeRow(row->next());
2782 status(bview, LyXText::NEED_MORE_REFRESH);
2784 breakAgainOneRow(bview, row);
2785 // will the cursor be in another row now?
2786 if (row->next() && row->next()->par() == row->par() &&
2787 rowLast(row) <= cursor.pos()) {
2789 breakAgainOneRow(bview, row);
2792 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2794 if (row->next() && row->next()->par() == row->par())
2795 need_break_row = row->next();
2799 // set the dimensions of the row
2800 row->fill(fill(bview, row, workWidth(bview)));
2801 int const tmpheight = row->height();
2802 setHeightOfRow(bview, row);
2803 if (tmpheight == row->height())
2804 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2806 status(bview, LyXText::NEED_MORE_REFRESH);
2809 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2813 // current_font = rawtmpfont;
2814 // real_current_font = realtmpfont;
2816 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2817 != cursor.boundary())
2818 setCursor(bview, cursor.par(), cursor.pos(), false,
2819 !cursor.boundary());
2821 lastpos = cursor.par()->size();
2822 if (cursor.pos() == lastpos)
2823 setCurrentFont(bview);
2825 // check, whether the last characters font has changed.
2827 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2828 redoHeightOfParagraph(bview, cursor);
2830 // now the special right address boxes
2831 if (textclasslist.Style(bview->buffer()->params.textclass,
2832 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2833 redoDrawingOfParagraph(bview, cursor);
2839 void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
2840 Row * row_ptr, int y, bool cleared)
2842 // returns a printed row
2843 Painter & pain = bview->painter();
2846 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
2848 Paragraph::size_type const last = rowLastPrintable(row_ptr);
2850 Paragraph::size_type vpos;
2851 Paragraph::size_type pos;
2855 LyXFont font(LyXFont::ALL_SANE);
2857 if (row_ptr->height() <= 0) {
2858 lyxerr << "LYX_ERROR: row.height: "
2859 << row_ptr->height() << endl;
2864 float fill_separator;
2866 float fill_label_hfill;
2867 prepareToPrint(bview, row_ptr, x, fill_separator,
2868 fill_hfill, fill_label_hfill);
2870 if (inset_owner && (x < 0))
2874 // clear the area where we want to paint/print
2875 int const ww = bview->workWidth();
2877 bool clear_area = true;
2880 if (!bview->screen()->forceClear() && last == row_ptr->pos()
2881 && row_ptr->par()->getChar(row_ptr->pos()) == Paragraph::META_INSET
2882 && (inset = row_ptr->par()->getInset(row_ptr->pos()))) {
2883 clear_area = inset->doClearArea();
2885 // we don't need to clear it's already done!!!
2888 } else if (clear_area) {
2889 #ifdef WITH_WARNINGS
2890 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
2891 #warning Should we not fix this in the Painter, please have a look Lars! (Jug)
2893 int const y = y_offset < 0 ? 0 : y_offset;
2894 int const h = y_offset < 0 ?
2895 row_ptr->height() + y_offset : row_ptr->height();
2896 int const w = inset_owner ?
2897 inset_owner->width(bview, font) - 2 : ww;
2898 int const x = x_offset;
2899 pain.fillRectangle(x, y, w, h);
2900 } else if (inset != 0) {
2901 int h = row_ptr->baseline() - inset->ascent(bview, font);
2903 int const w = (inset_owner ?
2904 inset_owner->width(bview, font) : ww);
2905 pain.fillRectangle(x_offset, y_offset, w, h);
2907 h += inset->ascent(bview, font) + inset->descent(bview, font);
2908 if ((row_ptr->height() - h) > 0) {
2909 int const w = (inset_owner ?
2910 inset_owner->width(bview, font) : ww);
2911 pain.fillRectangle(x_offset, y_offset + h,
2912 w, row_ptr->height() - h);
2914 if (!inset_owner && !inset->display() && !inset->needFullRow())
2916 int const w = inset->width(bview, font) + int(x);
2917 pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
2921 if (selection.set()) {
2922 int const w = (inset_owner ?
2923 inset_owner->width(bview, font) : ww);
2925 if (bidi_same_direction) {
2926 if (selection.start.row() == row_ptr &&
2927 selection.end.row() == row_ptr) {
2928 if (selection.start.x() < selection.end.x())
2929 pain.fillRectangle(x_offset + selection.start.x(),
2931 selection.end.x() - selection.start.x(),
2935 pain.fillRectangle(x_offset + selection.end.x(),
2937 selection.start.x() - selection.end.x(),
2940 } else if (selection.start.row() == row_ptr) {
2942 pain.fillRectangle(x_offset, y_offset,
2943 selection.start.x(),
2947 pain.fillRectangle(x_offset + selection.start.x(),
2949 w - selection.start.x(),
2952 } else if (selection.end.row() == row_ptr) {
2954 pain.fillRectangle(x_offset + selection.end.x(),
2956 w - selection.end.x(),
2960 pain.fillRectangle(x_offset, y_offset,
2964 } else if (y > selection.start.y()
2965 && y < selection.end.y()) {
2966 pain.fillRectangle(x_offset, y_offset, w,
2970 } else if (selection.start.row() != row_ptr &&
2971 selection.end.row() != row_ptr &&
2972 y > selection.start.y()
2973 && y < selection.end.y()) {
2974 pain.fillRectangle(x_offset, y_offset, w,
2977 } else if (selection.start.row() == row_ptr ||
2978 selection.end.row() == row_ptr) {
2980 if ((selection.start.row() != row_ptr && !is_rtl) ||
2981 (selection.end.row() != row_ptr && is_rtl))
2982 pain.fillRectangle(x_offset, y_offset,
2986 Paragraph::size_type main_body =
2987 beginningOfMainBody(bview->buffer(),
2990 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
2991 pos = vis2log(vpos);
2992 float const old_tmpx = tmpx;
2993 if (main_body > 0 && pos == main_body-1) {
2994 tmpx += fill_label_hfill +
2995 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
2996 row_ptr->par()->getLayout()).labelsep,
2997 getFont(bview->buffer(),row_ptr->par(), -2));
2998 if (row_ptr->par()->isLineSeparator(main_body-1))
2999 tmpx -= singleWidth(bview, row_ptr->par(), main_body-1);
3001 if (hfillExpansion(bview->buffer(), row_ptr, pos)) {
3002 tmpx += singleWidth(bview, row_ptr->par(), pos);
3003 if (pos >= main_body)
3006 tmpx += fill_label_hfill;
3008 else if (row_ptr->par()->isSeparator(pos)) {
3009 tmpx += singleWidth(bview, row_ptr->par(), pos);
3010 if (pos >= main_body)
3011 tmpx += fill_separator;
3013 tmpx += singleWidth(bview, row_ptr->par(), pos);
3015 if ((selection.start.row() != row_ptr ||
3016 selection.start.pos() <= pos) &&
3017 (selection.end.row() != row_ptr ||
3018 pos < selection.end.pos()) )
3019 // Here we do not use x_offset as x_offset was
3021 pain.fillRectangle(int(old_tmpx),
3023 int(tmpx - old_tmpx + 1),
3028 if ((selection.start.row() != row_ptr && is_rtl) ||
3029 (selection.end.row() != row_ptr && !is_rtl) )
3030 pain.fillRectangle(x_offset + int(tmpx),
3040 // Draw appendix lines
3041 Paragraph * firstpar = row_ptr->par();
3043 if (firstpar->params().appendix()) {
3044 pain.line(1, y_offset,
3045 1, y_offset + row_ptr->height(),
3046 LColor::appendixline);
3047 pain.line(ww - 2, y_offset,
3048 ww - 2, y_offset + row_ptr->height(),
3049 LColor::appendixline);
3053 Paragraph::depth_type const depth = firstpar->getDepth();
3055 Paragraph::depth_type next_depth = 0;
3056 Paragraph::depth_type prev_depth = 0;
3057 if (row_ptr->next())
3058 next_depth = row_ptr->next()->par()->getDepth();
3059 if (row_ptr->previous())
3060 prev_depth = row_ptr->previous()->par()->getDepth();
3062 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3063 int const line_x = (LYX_PAPER_MARGIN / 5) *
3064 i + box_x + x_offset;
3065 pain.line(line_x, y_offset, line_x,
3066 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3070 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3073 pain.fillRectangle(line_x,
3074 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3075 LYX_PAPER_MARGIN / 5, 2,
3081 LyXLayout const & layout =
3082 textclasslist.Style(bview->buffer()->params.textclass,
3083 row_ptr->par()->getLayout());
3086 int y_bottom = row_ptr->height();
3088 // is it a first row?
3089 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3091 // start of appendix?
3092 if (row_ptr->par()->params().startOfAppendix()) {
3093 pain.line(1, y_offset,
3095 LColor::appendixline);
3098 // think about the margins
3099 if (!row_ptr->previous() && bv_owner)
3100 y_top += LYX_PAPER_MARGIN;
3102 // draw a top pagebreak
3103 if (row_ptr->par()->params().pagebreakTop()) {
3105 pb_font.setColor(LColor::pagebreak).decSize();
3109 pain.line(0, y_offset + y_top + 2*defaultHeight(),
3111 y_offset + y_top + 2 * defaultHeight(),
3113 Painter::line_onoffdash)
3116 _("Page Break (top)"),
3119 LColor::background, false, w, a, d);
3120 pain.rectText((ww - w)/2,
3121 y_offset + y_top + 2 * defaultHeight() + d,
3122 _("Page Break (top)"),
3125 LColor::background);
3126 y_top += 3 * defaultHeight();
3129 if (row_ptr->par()->params().spaceTop().kind() == VSpace::VFILL) {
3131 pain.line(0, y_offset + 2 + y_top,
3132 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3135 pain.line(0, y_offset + y_top + 3 * defaultHeight(),
3137 y_offset + y_top + 3 * defaultHeight(),
3140 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3141 LYX_PAPER_MARGIN / 2,
3142 y_offset + y_top + 3 * defaultHeight(),
3145 y_top += 3 * defaultHeight();
3148 // think about user added space
3149 y_top += int(row_ptr->par()->params().spaceTop().inPixels(bview));
3151 // think about the parskip
3152 // some parskips VERY EASY IMPLEMENTATION
3153 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3154 if (layout.latextype == LATEX_PARAGRAPH
3155 && firstpar->getDepth() == 0
3156 && firstpar->previous())
3157 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3158 else if (firstpar->previous()
3159 && textclasslist.Style(bview->buffer()->params.textclass,
3160 firstpar->previous()->getLayout()).latextype == LATEX_PARAGRAPH
3161 && firstpar->previous()->getDepth() == 0)
3162 // is it right to use defskip here, too? (AS)
3163 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3166 if (row_ptr->par()->params().lineTop()) {
3168 y_top += lyxfont::ascent('x',
3169 getFont(bview->buffer(),
3170 row_ptr->par(), 0));
3171 int const w = (inset_owner ?
3172 inset_owner->width(bview, font) : ww);
3173 int const xp = static_cast<int>(inset_owner ? x : 0);
3174 pain.line(xp, y_offset + y_top,
3175 w, y_offset + y_top,
3177 Painter::line_solid,
3178 Painter::line_thick);
3180 y_top += lyxfont::ascent('x',getFont(bview->buffer(),
3181 row_ptr->par(), 0));
3184 // should we print a label?
3185 if (layout.labeltype >= LABEL_STATIC
3186 && (layout.labeltype != LABEL_STATIC
3187 || layout.latextype != LATEX_ENVIRONMENT
3188 || row_ptr->par()->isFirstInSequence())) {
3189 font = getFont(bview->buffer(), row_ptr->par(), -2);
3190 if (!row_ptr->par()->getLabelstring().empty()) {
3192 string const tmpstring =
3193 row_ptr->par()->getLabelstring();
3195 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3196 if (bview->buffer()->params.secnumdepth >= 0) {
3197 // this is special code for
3198 // the chapter layout. This is
3199 // printed in an extra row
3200 // and has a pagebreak at
3202 float spacing_val = 1.0;
3203 if (!row_ptr->par()->params().spacing().isDefault()) {
3204 spacing_val = row_ptr->par()->params().spacing().getValue();
3206 spacing_val = bview->buffer()->params.spacing.getValue();
3209 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3210 + int(layout.parsep) * defaultHeight();
3212 tmpx = ww - leftMargin(bview, row_ptr) -
3213 lyxfont::width(tmpstring, font);
3214 pain.text(int(tmpx),
3215 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3220 tmpx = ww - leftMargin(bview, row_ptr)
3221 + lyxfont::width(layout.labelsep, font);
3223 tmpx = x - lyxfont::width(layout.labelsep, font)
3224 - lyxfont::width(tmpstring, font);
3227 pain.text(int(tmpx),
3228 y_offset + row_ptr->baseline(),
3232 // the labels at the top of an environment.
3233 // More or less for bibliography
3234 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3235 layout.labeltype == LABEL_BIBLIO ||
3236 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3237 if (row_ptr->par()->isFirstInSequence()) {
3238 font = getFont(bview->buffer(),
3239 row_ptr->par(), -2);
3240 if (!row_ptr->par()->getLabelstring().empty()) {
3241 string const tmpstring =
3242 row_ptr->par()->getLabelstring();
3243 float spacing_val = 1.0;
3244 if (!row_ptr->par()->params().spacing().isDefault()) {
3245 spacing_val = row_ptr->par()->params().spacing().getValue();
3247 spacing_val = bview->buffer()->params.spacing.getValue();
3250 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3251 + (layout.labelbottomsep * defaultHeight()));
3254 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3255 tmpx = ( (is_rtl ? leftMargin(bview, row_ptr) : x)
3256 + ww - rightMargin(bview->buffer(), row_ptr) ) / 2;
3257 tmpx -= lyxfont::width(tmpstring, font) / 2;
3259 tmpx = ww - leftMargin(bview, row_ptr) -
3260 lyxfont::width(tmpstring, font);
3261 pain.text(int(tmpx),
3262 y_offset + row_ptr->baseline()
3263 - row_ptr->ascent_of_text()
3269 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3270 font = getFont(bview->buffer(), row_ptr->par(), -1);
3272 tmpx = ww - leftMargin(bview, row_ptr)
3273 + lyxfont::width(layout.labelsep, font);
3275 tmpx = x - lyxfont::width(layout.labelsep, font)
3276 - row_ptr->par()->bibkey->width(bview, font);
3277 row_ptr->par()->bibkey->draw(bview, font,
3278 y_offset + row_ptr->baseline(),
3283 // is it a last row?
3284 Paragraph * par = row_ptr->par();
3285 if (row_ptr->par() == par
3286 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
3287 // think about the margins
3288 if (!row_ptr->next() && bv_owner)
3289 y_bottom -= LYX_PAPER_MARGIN;
3291 // draw a bottom pagebreak
3292 if (firstpar->params().pagebreakBottom()) {
3294 pb_font.setColor(LColor::pagebreak).decSize();
3295 int const y_place = y_offset + y_bottom
3296 - 2 * defaultHeight();
3302 .line(0, y_place, ww, y_place,
3304 Painter::line_onoffdash)
3306 _("Page Break (bottom)"),
3309 LColor::background, false, w, a, d);
3310 pain.rectText((ww - w) / 2, y_place + d,
3311 _("Page Break (bottom)"),
3314 LColor::background);
3315 y_bottom -= 3 * defaultHeight();
3318 if (firstpar->params().spaceBottom().kind() == VSpace::VFILL) {
3319 // draw a vfill bottom
3320 int const y_place = y_offset + y_bottom
3321 - 3 * defaultHeight();
3323 pain.line(0, y_place,
3324 LYX_PAPER_MARGIN, y_place,
3326 pain.line(0, y_offset + y_bottom - 2,
3328 y_offset + y_bottom - 2,
3330 pain.line(LYX_PAPER_MARGIN / 2,
3332 LYX_PAPER_MARGIN / 2,
3333 y_offset + y_bottom - 2,
3335 y_bottom -= 3 * defaultHeight();
3338 // think about user added space
3339 y_bottom -= int(firstpar->params().spaceBottom().inPixels(bview));
3341 if (firstpar->params().lineBottom()) {
3342 // draw a bottom line
3343 y_bottom -= lyxfont::ascent('x',
3344 getFont(bview->buffer(),
3347 int const w = (inset_owner ?
3348 inset_owner->width(bview, font) : ww);
3349 int const xp = static_cast<int>(inset_owner ? x : 0);
3350 pain.line(xp, y_offset + y_bottom,
3351 w, y_offset + y_bottom,
3352 LColor::topline, Painter::line_solid,
3353 Painter::line_thick);
3354 y_bottom -= lyxfont::ascent('x',
3355 getFont(bview->buffer(),
3361 int const endlabel =
3362 row_ptr->par()->getEndLabel(bview->buffer()->params);
3365 case END_LABEL_FILLED_BOX:
3367 LyXFont const font = getFont(bview->buffer(),
3368 row_ptr->par(), last);
3369 int const size = int(0.75 * lyxfont::maxAscent(font));
3370 int const y = (y_offset + row_ptr->baseline()) - size;
3371 int x = is_rtl ? LYX_PAPER_MARGIN
3372 : ww - LYX_PAPER_MARGIN - size;
3374 if (row_ptr->fill() <= size)
3375 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3376 if (endlabel == END_LABEL_BOX) {
3377 pain.line(x, y, x, y + size,
3379 pain.line(x + size, y, x + size , y + size,
3381 pain.line(x, y, x + size, y,
3383 pain.line(x, y + size, x + size, y + size,
3386 pain.fillRectangle(x, y, size, size,
3390 case END_LABEL_STATIC:
3392 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->getLayout();
3393 string const tmpstring = textclasslist.
3394 Style(bview->buffer()->params.textclass,
3395 layout).endlabelstring();
3396 font = getFont(bview->buffer(), row_ptr->par(), -2);
3397 int const tmpx = is_rtl ?
3398 int(x) - lyxfont::width(tmpstring, font)
3399 : ww - rightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
3400 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
3403 case END_LABEL_NO_LABEL:
3408 // draw the text in the pixmap
3410 vpos = row_ptr->pos();
3412 Paragraph::size_type main_body =
3413 beginningOfMainBody(bview->buffer(), row_ptr->par());
3414 if (main_body > 0 &&
3415 (main_body-1 > last ||
3416 !row_ptr->par()->isLineSeparator(main_body - 1)))
3419 while (vpos <= last) {
3420 pos = vis2log(vpos);
3421 if (main_body > 0 && pos == main_body - 1) {
3422 x += fill_label_hfill
3423 + lyxfont::width(layout.labelsep,
3424 getFont(bview->buffer(),
3425 row_ptr->par(), -2))
3426 - singleWidth(bview,
3431 if (row_ptr->par() ->isHfill(pos)) {
3434 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3436 y_offset + row_ptr->baseline(),
3439 if (hfillExpansion(bview->buffer(),
3441 if (pos >= main_body) {
3443 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3444 int(x + fill_hfill),
3445 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3447 Painter::line_onoffdash);
3451 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3452 int(x + fill_label_hfill),
3453 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3455 Painter::line_onoffdash);
3457 x += fill_label_hfill;
3460 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3462 y_offset + row_ptr->baseline(),
3467 } else if (row_ptr->par()->isSeparator(pos)) {
3468 x += singleWidth(bview,
3469 row_ptr->par(), pos);
3470 if (pos >= main_body)
3471 x += fill_separator;
3474 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
3479 int LyXText::defaultHeight() const
3481 LyXFont font(LyXFont::ALL_SANE);
3482 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3486 /* returns the column near the specified x-coordinate of the row
3487 * x is set to the real beginning of this column */
3488 int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3489 bool & boundary) const
3492 float fill_separator;
3494 float fill_label_hfill;
3496 prepareToPrint(bview, row, tmpx, fill_separator,
3497 fill_hfill, fill_label_hfill);
3499 Paragraph::size_type vc = row->pos();
3500 Paragraph::size_type last = rowLastPrintable(row);
3501 Paragraph::size_type c = 0;
3502 LyXLayout const & layout =
3503 textclasslist.Style(bview->buffer()->params.textclass,
3504 row->par()->getLayout());
3505 bool left_side = false;
3507 Paragraph::size_type
3508 main_body = beginningOfMainBody(bview->buffer(), row->par());
3509 float last_tmpx = tmpx;
3511 if (main_body > 0 &&
3512 (main_body - 1 > last ||
3513 !row->par()->isLineSeparator(main_body - 1)))
3516 while (vc <= last && tmpx <= x) {
3519 if (main_body > 0 && c == main_body-1) {
3520 tmpx += fill_label_hfill +
3521 lyxfont::width(layout.labelsep,
3522 getFont(bview->buffer(), row->par(), -2));
3523 if (row->par()->isLineSeparator(main_body - 1))
3524 tmpx -= singleWidth(bview, row->par(), main_body-1);
3527 if (hfillExpansion(bview->buffer(), row, c)) {
3528 x += singleWidth(bview, row->par(), c);
3532 tmpx += fill_label_hfill;
3534 else if (row->par()->isSeparator(c)) {
3535 tmpx += singleWidth(bview, row->par(), c);
3537 tmpx+= fill_separator;
3539 tmpx += singleWidth(bview, row->par(), c);
3543 if ((tmpx + last_tmpx) / 2 > x) {
3548 if (vc > last + 1) // This shouldn't happen.
3552 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3553 // some speedup if rtl_support=false
3554 && (!row->next() || row->next()->par() != row->par());
3555 bool const rtl = (lastrow)
3556 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3557 : false; // If lastrow is false, we don't need to compute
3558 // the value of rtl.
3560 if (row->pos() > last) // Row is empty?
3563 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3564 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3566 else if (vc == row->pos()) {
3568 if (bidi_level(c) % 2 == 1)
3571 c = vis2log(vc - 1);
3572 bool const rtl = (bidi_level(c) % 2 == 1);
3573 if (left_side == rtl) {
3575 boundary = isBoundary(bview->buffer(), row->par(), c);
3579 if (row->pos() <= last && c > last
3580 && row->par()->isNewline(last)) {
3581 if (bidi_level(last) % 2 == 0)
3582 tmpx -= singleWidth(bview, row->par(), last);
3584 tmpx += singleWidth(bview, row->par(), last);
3594 // returns pointer to a specified row
3595 Row * LyXText::getRow(Paragraph * par,
3596 Paragraph::size_type pos, int & y) const
3601 Row * tmprow = firstrow;
3604 // find the first row of the specified paragraph
3605 while (tmprow->next() && tmprow->par() != par) {
3606 y += tmprow->height();
3607 tmprow = tmprow->next();
3610 // now find the wanted row
3611 while (tmprow->pos() < pos
3613 && tmprow->next()->par() == par
3614 && tmprow->next()->pos() <= pos) {
3615 y += tmprow->height();
3616 tmprow = tmprow->next();