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 * ====================================================== */
14 #include "paragraph.h"
16 #include "bufferparams.h"
22 #include "frontends/LyXView.h"
23 #include "frontends/Painter.h"
24 #include "frontends/font_metrics.h"
25 #include "frontends/screen.h"
26 #include "frontends/WorkArea.h"
27 #include "bufferview_funcs.h"
28 #include "BufferView.h"
30 #include "ParagraphParameters.h"
31 #include "undo_funcs.h"
32 #include "text_funcs.h"
33 #include "WordLangTuple.h"
34 #include "paragraph_funcs.h"
35 #include "rowpainter.h"
36 #include "lyxrow_funcs.h"
37 #include "metricsinfo.h"
39 #include "insets/insettext.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
47 using namespace lyx::support;
55 using lyx::word_location;
57 using namespace bv_funcs;
59 /// top, right, bottom pixel margin
60 extern int const PAPER_MARGIN = 20;
61 /// margin for changebar
62 extern int const CHANGEBAR_MARGIN = 10;
64 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
66 int bibitemMaxWidth(BufferView *, LyXFont const &);
69 BufferView * LyXText::bv()
71 Assert(bv_owner != 0);
76 BufferView * LyXText::bv() const
78 Assert(bv_owner != 0);
83 void LyXText::updateRowPositions()
85 ParagraphList::iterator pit = ownerParagraphs().begin();
86 ParagraphList::iterator end = ownerParagraphs().end();
87 for (int y = 0; pit != end; ++pit) {
88 RowList::iterator rit = pit->rows.begin();
89 RowList::iterator rend = pit->rows.end();
90 for ( ; rit != rend ; rit = ++rit) {
98 int LyXText::top_y() const
100 if (anchor_row_ == endRow())
103 return anchor_row_->y() + anchor_row_offset_;
107 void LyXText::top_y(int newy)
109 if (ownerParagraphs().begin()->rows.empty())
113 anchor_row_ = firstRow();
114 anchor_row_offset_ = newy;
118 lyxerr[Debug::GUI] << "setting top y = " << newy << endl;
121 ParagraphList::iterator dummypit;
122 anchor_row_ = getRowNearY(y, dummypit);
123 anchor_row_offset_ = newy - y;
124 lyxerr[Debug::GUI] << "changing reference to row: " << &*anchor_row_
125 << " offset: " << anchor_row_offset_ << endl;
129 void LyXText::anchor_row(RowList::iterator rit)
132 anchor_row_offset_ = 0;
134 anchor_row_offset_ = old_y - top_y();
135 lyxerr[Debug::GUI] << "anchor_row(): changing reference to row: "
136 << &*anchor_row_ << " offset: "
137 << anchor_row_offset_ << endl;
141 int LyXText::workWidth() const
143 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
147 int LyXText::getRealCursorX() const
150 if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
151 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
156 #warning FIXME This function seems to belong outside of LyxText.
157 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
160 if (!Encodings::is_arabic(c))
161 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
162 return c + (0xb0 - '0');
166 unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
167 unsigned char next_char = ' ';
169 pos_type const par_size = par.size();
171 for (pos_type i = pos + 1; i < par_size; ++i) {
172 unsigned char const par_char = par.getChar(i);
173 if (!Encodings::IsComposeChar_arabic(par_char)) {
174 next_char = par_char;
179 if (Encodings::is_arabic(next_char)) {
180 if (Encodings::is_arabic(prev_char) &&
181 !Encodings::is_arabic_special(prev_char))
182 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
184 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
186 if (Encodings::is_arabic(prev_char) &&
187 !Encodings::is_arabic_special(prev_char))
188 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
190 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
194 // This is the comments that some of the warnings below refers to.
195 // There are some issues in this file and I don't think they are
196 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
197 // this is a problem that has been here almost from day one and that a
198 // larger userbase with differenct access patters triggers the bad
199 // behaviour. (segfaults.) What I think happen is: In several places
200 // we store the paragraph in the current cursor and then moves the
201 // cursor. This movement of the cursor will delete paragraph at the
202 // old position if it is now empty. This will make the temporary
203 // pointer to the old cursor paragraph invalid and dangerous to use.
204 // And is some cases this will trigger a segfault. I have marked some
205 // of the cases where this happens with a warning, but I am sure there
206 // are others in this file and in text2.C. There is also a note in
207 // Delete() that you should read. In Delete I store the paragraph->id
208 // instead of a pointer to the paragraph. I am pretty sure this faulty
209 // use of temporary pointers to paragraphs that might have gotten
210 // invalidated (through a cursor movement) before they are used, are
211 // the cause of the strange crashes we get reported often.
213 // It is very tiresom to change this code, especially when it is as
214 // hard to read as it is. Help to fix all the cases where this is done
215 // would be greately appreciated.
219 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
221 if (pos >= pit->size())
224 char const c = pit->getChar(pos);
225 return singleWidth(pit, pos, c);
229 int LyXText::singleWidth(ParagraphList::iterator pit,
230 pos_type pos, char c) const
232 if (pos >= pit->size())
235 LyXFont const & font = getFont(pit, pos);
237 // The most common case is handled first (Asger)
238 if (IsPrintable(c)) {
239 if (font.language()->RightToLeft()) {
240 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
241 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
242 && font.language()->lang() == "arabic") {
243 if (Encodings::IsComposeChar_arabic(c))
246 c = transformChar(c, *pit, pos);
247 } else if (font.language()->lang() == "hebrew" &&
248 Encodings::IsComposeChar_hebrew(c))
251 return font_metrics::width(c, font);
254 if (c == Paragraph::META_INSET) {
255 InsetOld * tmpinset = pit->getInset(pos);
257 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
258 // Because of the representation as vertical lines
262 #warning enabling this fixes the 'insets of width 0 on load' problem
263 // this IS needed otherwise on initialitation we don't get the fill
264 // of the row right (ONLY on initialization if we read a file!)
265 // should be changed! (Jug 20011204)
266 //tmpinset->update(bv());
268 MetricsInfo mi(bv(), font, workWidth());
269 tmpinset->metrics(mi, dim);
272 return tmpinset->width();
278 if (IsSeparatorChar(c))
280 return font_metrics::width(c, font);
284 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
286 if (bidi_start == -1)
289 return log2vis_list[pos - bidi_start];
293 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
295 if (bidi_start == -1)
298 return vis2log_list[pos - bidi_start];
302 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
304 if (bidi_start == -1)
307 return bidi_levels[pos - bidi_start];
311 bool LyXText::bidi_InRange(lyx::pos_type pos) const
313 return bidi_start == -1 ||
314 (bidi_start <= pos && pos <= bidi_end);
318 void LyXText::computeBidiTables(ParagraphList::iterator pit,
319 Buffer const * buf, RowList::iterator row) const
321 bidi_same_direction = true;
322 if (!lyxrc.rtl_support) {
327 InsetOld * inset = pit->inInset();
328 if (inset && inset->owner() &&
329 inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
334 bidi_start = row->pos();
335 bidi_end = lastPrintablePos(*pit, row);
337 if (bidi_start > bidi_end) {
342 if (bidi_end + 2 - bidi_start >
343 static_cast<pos_type>(log2vis_list.size())) {
345 (bidi_end + 2 - bidi_start < 500) ?
346 500 : 2 * (bidi_end + 2 - bidi_start);
347 log2vis_list.resize(new_size);
348 vis2log_list.resize(new_size);
349 bidi_levels.resize(new_size);
352 vis2log_list[bidi_end + 1 - bidi_start] = -1;
353 log2vis_list[bidi_end + 1 - bidi_start] = -1;
357 pit->isRightToLeftPar(buf->params);
361 pos_type const body_pos = pit->beginningOfBody();
363 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
364 bool is_space = pit->isLineSeparator(lpos);
366 (is_space && lpos + 1 <= bidi_end &&
367 !pit->isLineSeparator(lpos + 1) &&
368 !pit->isNewline(lpos + 1))
370 LyXFont font = pit->getFontSettings(buf->params, pos);
371 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
372 font.number() == LyXFont::ON &&
373 pit->getFontSettings(buf->params, lpos - 1).number()
375 font = pit->getFontSettings(buf->params, lpos);
380 bool new_rtl = font.isVisibleRightToLeft();
381 bool new_rtl0 = font.isRightToLeft();
384 if (lpos == body_pos - 1
385 && row->pos() < body_pos - 1
387 new_level = (rtl_par) ? 1 : 0;
388 new_rtl = new_rtl0 = rtl_par;
390 new_level = (new_rtl) ? 1 : 2;
392 new_level = (rtl_par) ? 2 : 0;
394 if (is_space && new_level >= level) {
400 int new_level2 = new_level;
402 if (level == new_level && rtl0 != new_rtl0) {
404 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
405 } else if (level < new_level) {
406 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
407 if (new_level > rtl_par)
408 bidi_same_direction = false;
410 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
413 bidi_levels[lpos - bidi_start] = new_level;
415 while (level > new_level2) {
416 pos_type old_lpos = stack[--level];
417 int delta = lpos - old_lpos - 1;
420 log2vis_list[lpos - bidi_start] += delta;
421 log2vis_list[old_lpos - bidi_start] += delta;
423 while (level < new_level)
424 stack[level++] = lpos;
428 pos_type const old_lpos = stack[--level];
429 int delta = bidi_end - old_lpos;
432 log2vis_list[old_lpos - bidi_start] += delta;
435 pos_type vpos = bidi_start - 1;
436 for (pos_type lpos = bidi_start;
437 lpos <= bidi_end; ++lpos) {
438 vpos += log2vis_list[lpos - bidi_start];
439 vis2log_list[vpos - bidi_start] = lpos;
440 log2vis_list[lpos - bidi_start] = vpos;
445 // This method requires a previous call to ComputeBidiTables()
446 bool LyXText::isBoundary(Buffer const * buf, Paragraph const & par,
449 if (!lyxrc.rtl_support || pos == 0)
452 if (!bidi_InRange(pos - 1)) {
453 /// This can happen if pos is the first char of a row.
454 /// Returning false in this case is incorrect!
458 bool const rtl = bidi_level(pos - 1) % 2;
459 bool const rtl2 = bidi_InRange(pos)
460 ? bidi_level(pos) % 2
461 : par.isRightToLeftPar(buf->params);
466 bool LyXText::isBoundary(Buffer const * buf, Paragraph const & par,
467 pos_type pos, LyXFont const & font) const
469 if (!lyxrc.rtl_support)
470 return false; // This is just for speedup
472 bool const rtl = font.isVisibleRightToLeft();
473 bool const rtl2 = bidi_InRange(pos)
474 ? bidi_level(pos) % 2
475 : par.isRightToLeftPar(buf->params);
480 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
484 if (row.pos() < pit->size())
485 if (pit->getChar(row.pos()) == Paragraph::META_INSET &&
486 (ins = pit->getInset(row.pos())) &&
487 (ins->needFullRow() || ins->display()))
490 LyXTextClass const & tclass =
491 bv()->buffer()->params.getLyXTextClass();
492 LyXLayout_ptr const & layout = pit->layout();
494 string parindent = layout->parindent;
498 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
500 // this is the way, LyX handles the LaTeX-Environments.
501 // I have had this idea very late, so it seems to be a
502 // later added hack and this is true
503 if (!pit->getDepth()) {
504 if (pit->layout() == tclass.defaultLayout()) {
505 // find the previous same level paragraph
506 if (pit != ownerParagraphs().begin()) {
507 ParagraphList::iterator newpit =
508 depthHook(pit, ownerParagraphs(),
511 newpit->layout()->nextnoindent)
516 // find the next level paragraph
518 ParagraphList::iterator newpar = outerHook(pit,
521 // make a corresponding row. Needed to call leftMargin()
523 // check wether it is a sufficent paragraph
524 if (newpar != ownerParagraphs().end() &&
525 newpar->layout()->isEnvironment()) {
526 x = leftMargin(newpar, Row(newpar->size()));
529 if (newpar != ownerParagraphs().end() &&
530 pit->layout() == tclass.defaultLayout()) {
531 if (newpar->params().noindent())
534 parindent = newpar->layout()->parindent;
540 LyXFont const labelfont = getLabelFont(pit);
541 switch (layout->margintype) {
543 if (!layout->leftmargin.empty()) {
544 x += font_metrics::signedWidth(layout->leftmargin,
545 tclass.defaultfont());
547 if (!pit->getLabelstring().empty()) {
548 x += font_metrics::signedWidth(layout->labelindent,
550 x += font_metrics::width(pit->getLabelstring(),
552 x += font_metrics::width(layout->labelsep, labelfont);
556 x += font_metrics::signedWidth(layout->labelindent, labelfont);
557 // The width of an empty par, even with manual label, should be 0
558 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
559 if (!pit->getLabelWidthString().empty()) {
560 x += font_metrics::width(pit->getLabelWidthString(),
562 x += font_metrics::width(layout->labelsep, labelfont);
567 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
568 / (pit->getDepth() + 4);
570 case MARGIN_FIRST_DYNAMIC:
571 if (layout->labeltype == LABEL_MANUAL) {
572 if (row.pos() >= pit->beginningOfBody()) {
573 x += font_metrics::signedWidth(layout->leftmargin,
576 x += font_metrics::signedWidth(layout->labelindent,
580 // Special case to fix problems with
582 || (layout->labeltype == LABEL_STATIC
583 && layout->latextype == LATEX_ENVIRONMENT
584 && !isFirstInSequence(pit, ownerParagraphs()))) {
585 x += font_metrics::signedWidth(layout->leftmargin,
587 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
588 && layout->labeltype != LABEL_BIBLIO
589 && layout->labeltype !=
590 LABEL_CENTERED_TOP_ENVIRONMENT) {
591 x += font_metrics::signedWidth(layout->labelindent,
593 x += font_metrics::width(layout->labelsep, labelfont);
594 x += font_metrics::width(pit->getLabelstring(),
599 case MARGIN_RIGHT_ADDRESS_BOX:
601 // ok, a terrible hack. The left margin depends on the widest
602 // row in this paragraph. Do not care about footnotes, they
603 // are *NOT* allowed in the LaTeX realisation of this layout.
605 // find the first row of this paragraph
606 RowList::iterator rit = pit->rows.begin();
607 RowList::iterator end = pit->rows.end();
608 int minfill = rit->fill();
609 for ( ; rit != end; ++rit)
610 if (rit->fill() < minfill)
611 minfill = rit->fill();
613 x += font_metrics::signedWidth(layout->leftmargin,
614 tclass.defaultfont());
620 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
621 LyXLength const len = pit->params().leftIndent();
622 int const tw = inset_owner ?
623 inset_owner->latexTextWidth(bv()) : workWidth();
624 x += len.inPixels(tw);
629 if (pit->params().align() == LYX_ALIGN_LAYOUT)
630 align = layout->align;
632 align = pit->params().align();
634 // set the correct parindent
635 if (row.pos() == 0) {
636 if ((layout->labeltype == LABEL_NO_LABEL
637 || layout->labeltype == LABEL_TOP_ENVIRONMENT
638 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
639 || (layout->labeltype == LABEL_STATIC
640 && layout->latextype == LATEX_ENVIRONMENT
641 && !isFirstInSequence(pit, ownerParagraphs())))
642 && align == LYX_ALIGN_BLOCK
643 && !pit->params().noindent()
644 // in tabulars and ert paragraphs are never indented!
645 && (!pit->inInset() || !pit->inInset()->owner() ||
646 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
647 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
648 && (pit->layout() != tclass.defaultLayout() ||
649 bv()->buffer()->params.paragraph_separation ==
650 BufferParams::PARSEP_INDENT)) {
651 x += font_metrics::signedWidth(parindent,
652 tclass.defaultfont());
653 } else if (layout->labeltype == LABEL_BIBLIO) {
654 // ale970405 Right width for bibitems
655 x += bibitemMaxWidth(bv(), tclass.defaultfont());
663 int LyXText::rightMargin(ParagraphList::iterator pit,
664 Buffer const & buf, Row const & row) const
668 if (row.pos() < pit->size())
669 if ((pit->getChar(row.pos()) == Paragraph::META_INSET) &&
670 (ins = pit->getInset(row.pos())) &&
671 (ins->needFullRow() || ins->display()))
674 LyXTextClass const & tclass = buf.params.getLyXTextClass();
675 LyXLayout_ptr const & layout = pit->layout();
678 + font_metrics::signedWidth(tclass.rightmargin(),
679 tclass.defaultfont());
680 + font_metrics::signedWidth(layout->rightmargin,
681 tclass.defaultfont())
682 * 4 / (pit->getDepth() + 4);
686 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
688 if (pit->layout()->margintype == MARGIN_MANUAL) {
690 tmprow.pos(pit->size());
691 // return the beginning of the body
692 return leftMargin(pit, tmprow);
695 // LabelEnd is only needed if the layout
696 // fills a flushleft label.
703 // this needs special handling - only newlines count as a break point
704 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
706 for (; i < par.size(); ++i) {
707 if (par.isNewline(i))
717 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
718 Row const & row) const
720 // maximum pixel width of a row.
721 int width = workWidth() - rightMargin(pit, *bv()->buffer(), row);
723 // inset->textWidth() returns -1 via workWidth(),
728 LyXLayout_ptr const & layout = pit->layout();
730 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
731 return addressBreakPoint(row.pos(), *pit);
733 pos_type const pos = row.pos();
734 pos_type const body_pos = pit->beginningOfBody();
735 pos_type const last = pit->size();
736 pos_type point = last;
741 // Now we iterate through until we reach the right margin
742 // or the end of the par, then choose the possible break
745 int const left = leftMargin(pit, row);
748 // pixel width since last breakpoint
750 bool fullrow = false;
754 // We re-use the font resolution for the entire font span when possible
755 LyXFont font = getFont(pit, i);
756 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
758 for (; i < last; ++i) {
759 if (pit->isNewline(i)) {
764 char const c = pit->getChar(i);
768 // add the auto-hfill from label end to the body
769 if (body_pos && i == body_pos) {
770 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
771 if (pit->isLineSeparator(i - 1))
772 thiswidth -= singleWidth(pit, i - 1);
773 int left_margin = labelEnd(pit, row);
774 if (thiswidth + x < left_margin)
775 thiswidth = left_margin - x;
776 thiswidth += singleWidth(pit, i, c);
778 // Manual inlined optimised version of common case of "thiswidth = singleWidth(pit, i, c);"
779 if (IsPrintable(c)) {
780 if (pos > endPosOfFontSpan) {
781 // We need to get the next font
782 font = getFont(pit, i);
783 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
785 if (! font.language()->RightToLeft()) {
786 thiswidth = font_metrics::width(c, font);
788 // Fall-back to normal case
789 thiswidth = singleWidth(pit, i, c);
790 // And flush font cache
791 endPosOfFontSpan = 0;
794 // Fall-back to normal case
795 thiswidth = singleWidth(pit, i, c);
796 // And flush font cache
797 endPosOfFontSpan = 0;
802 chunkwidth += thiswidth;
804 InsetOld * in = pit->isInset(i) ? pit->getInset(i) : 0;
805 fullrow = in && (in->display() || in->needFullRow());
807 // break before a character that will fall off
808 // the right of the row
810 // if no break before or we are at an inset
811 // that will take up a row, break here
812 if (point == last || fullrow || chunkwidth >= (width - left)) {
821 if (!in || in->isChar()) {
822 // some insets are line separators too
823 if (pit->isLineSeparator(i)) {
833 // full row insets start at a new row
835 if (pos < last - 1) {
837 if (pit->isLineSeparator(i + 1))
840 // to avoid extra rows
850 if (point == last && x >= width) {
851 // didn't find one, break at the point we reached the edge
853 } else if (i == last && x < width) {
854 // found one, but we fell off the end of the par, so prefer
859 // manual labels cannot be broken in LaTeX. But we
860 // want to make our on-screen rendering of footnotes
862 if (!fullrow && body_pos && point < body_pos)
863 point = body_pos - 1;
869 // returns the minimum space a row needs on the screen in pixel
870 int LyXText::fill(ParagraphList::iterator pit,
871 RowList::iterator row, int paper_width) const
877 // get the pure distance
878 pos_type const last = lastPrintablePos(*pit, row);
880 LyXLayout_ptr const & layout = pit->layout();
882 // special handling of the right address boxes
883 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
884 int const tmpfill = row->fill();
885 row->fill(0); // the minfill in MarginLeft()
886 w = leftMargin(pit, *row);
889 w = leftMargin(pit, *row);
891 pos_type const body_pos = pit->beginningOfBody();
892 pos_type i = row->pos();
894 if (! pit->empty() && i <= last) {
895 // We re-use the font resolution for the entire span when possible
896 LyXFont font = getFont(pit, i);
897 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
899 if (body_pos > 0 && i == body_pos) {
900 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
901 if (pit->isLineSeparator(i - 1))
902 w -= singleWidth(pit, i - 1);
903 int left_margin = labelEnd(pit, *row);
907 { // Manual inlined an optimised version of the common case of "w += singleWidth(pit, i);"
908 char const c = pit->getChar(i);
910 if (IsPrintable(c)) {
911 if (i > endPosOfFontSpan) {
912 // We need to get the next font
913 font = getFont(pit, i);
914 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
916 if (!font.language()->RightToLeft()) {
917 w += font_metrics::width(c, font);
919 // Fall-back to the normal case
920 w += singleWidth(pit, i, c);
921 // And flush font cache
922 endPosOfFontSpan = 0;
925 // Fall-back to the normal case
926 w += singleWidth(pit, i, c);
927 // And flush font cache
928 endPosOfFontSpan = 0;
934 if (body_pos > 0 && body_pos > last) {
935 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
936 if (last >= 0 && pit->isLineSeparator(last))
937 w -= singleWidth(pit, last);
938 int const left_margin = labelEnd(pit, *row);
943 int const fill = paper_width - w - rightMargin(pit, *bv()->buffer(), *row);
945 // If this case happens, it means that our calculation
946 // of the widths of the chars when we do rowBreakPoint()
947 // went wrong for some reason. Typically in list bodies.
948 // Things just about hobble on anyway, though you'll end
949 // up with a "fill_separator" less than zero, which corresponds
950 // to inter-word spacing being too small. Hopefully this problem
951 // will die when the label hacks die.
952 if (lyxerr.debugging() && fill < 0) {
953 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
954 << " w " << w << " paper_width " << paper_width
955 << " right margin " << rightMargin(pit, *bv()->buffer(), *row) << endl;
961 // returns the minimum space a manual label needs on the screen in pixel
962 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
964 pos_type last = pit->beginningOfBody();
968 // -1 because a label ends either with a space that is in the label,
969 // or with the beginning of a footnote that is outside the label.
972 // a separator at this end does not count
973 if (pit->isLineSeparator(last))
977 pos_type i = row.pos();
979 w += singleWidth(pit, i);
984 string const & labwidstr = pit->params().labelWidthString();
985 if (!labwidstr.empty()) {
986 LyXFont const labfont = getLabelFont(pit);
987 int const labwidth = font_metrics::width(labwidstr, labfont);
988 fill = max(labwidth - w, 0);
995 LColor::color LyXText::backgroundColor() const
998 return inset_owner->backgroundColor();
1000 return LColor::background;
1004 void LyXText::setHeightOfRow(ParagraphList::iterator pit, RowList::iterator rit)
1006 // get the maximum ascent and the maximum descent
1007 double layoutasc = 0;
1008 double layoutdesc = 0;
1011 // ok, let us initialize the maxasc and maxdesc value.
1012 // Only the fontsize count. The other properties
1013 // are taken from the layoutfont. Nicer on the screen :)
1014 LyXLayout_ptr const & layout = pit->layout();
1016 // as max get the first character of this row then it can increase but not
1017 // decrease the height. Just some point to start with so we don't have to
1018 // do the assignment below too often.
1019 LyXFont font = getFont(pit, rit->pos());
1020 LyXFont::FONT_SIZE const tmpsize = font.size();
1021 font = getLayoutFont(pit);
1022 LyXFont::FONT_SIZE const size = font.size();
1023 font.setSize(tmpsize);
1025 LyXFont labelfont = getLabelFont(pit);
1027 double spacing_val = 1.0;
1028 if (!pit->params().spacing().isDefault())
1029 spacing_val = pit->params().spacing().getValue();
1031 spacing_val = bv()->buffer()->params.spacing.getValue();
1032 //lyxerr << "spacing_val = " << spacing_val << endl;
1034 int maxasc = int(font_metrics::maxAscent(font) *
1035 layout->spacing.getValue() * spacing_val);
1036 int maxdesc = int(font_metrics::maxDescent(font) *
1037 layout->spacing.getValue() * spacing_val);
1039 pos_type const pos_end = lastPos(*pit, rit);
1043 if (!pit->empty()) {
1044 // We re-use the font resolution for the entire font span when possible
1045 LyXFont font = getFont(pit, rit->pos());
1046 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(rit->pos());
1049 Paragraph const & par = *pit;
1051 // Check if any insets are larger
1052 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
1053 // Manual inlined optimised version of common case of
1054 // "maxwidth += singleWidth(pit, pos);"
1055 char const c = par.getChar(pos);
1057 if (IsPrintable(c)) {
1058 if (pos > endPosOfFontSpan) {
1059 // We need to get the next font
1060 font = getFont(pit, pos);
1061 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
1063 if (! font.language()->RightToLeft()) {
1064 maxwidth += font_metrics::width(c, font);
1066 // Fall-back to normal case
1067 maxwidth += singleWidth(pit, pos, c);
1068 // And flush font cache
1069 endPosOfFontSpan = 0;
1072 // Special handling of insets - are any larger?
1073 if (par.isInset(pos)) {
1074 InsetOld const * tmpinset = par.getInset(pos);
1076 #if 1 // this is needed for deep update on initialitation
1077 #warning inset->update FIXME
1078 //tmpinset->update(bv());
1079 LyXFont const tmpfont = getFont(pit, pos);
1081 MetricsInfo mi(bv(), tmpfont, workWidth());
1082 tmpinset->metrics(mi, dim);
1083 maxwidth += dim.wid;
1084 maxasc = max(maxasc, dim.asc);
1085 maxdesc = max(maxdesc, dim.des);
1087 maxwidth += tmpinset->width();
1088 maxasc = max(maxasc, tmpinset->ascent());
1089 maxdesc = max(maxdesc, tmpinset->descent());
1093 // Fall-back to normal case
1094 maxwidth += singleWidth(pit, pos, c);
1095 // And flush font cache
1096 endPosOfFontSpan = 0;
1102 // Check if any custom fonts are larger (Asger)
1103 // This is not completely correct, but we can live with the small,
1104 // cosmetic error for now.
1105 LyXFont::FONT_SIZE maxsize =
1106 pit->highestFontInRange(rit->pos(), pos_end, size);
1107 if (maxsize > font.size()) {
1108 font.setSize(maxsize);
1109 maxasc = max(maxasc, font_metrics::maxAscent(font));
1110 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
1113 // This is nicer with box insets:
1117 rit->ascent_of_text(maxasc);
1119 // is it a top line?
1122 // some parksips VERY EASY IMPLEMENTATION
1123 if (bv()->buffer()->params.paragraph_separation ==
1124 BufferParams::PARSEP_SKIP)
1126 if (layout->isParagraph()
1127 && pit->getDepth() == 0
1128 && pit != ownerParagraphs().begin())
1130 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1131 } else if (pit != ownerParagraphs().begin() &&
1132 boost::prior(pit)->layout()->isParagraph() &&
1133 boost::prior(pit)->getDepth() == 0)
1135 // is it right to use defskip here too? (AS)
1136 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1141 if (pit == ownerParagraphs().begin() && !isInInset())
1142 maxasc += PAPER_MARGIN;
1144 // add the vertical spaces, that the user added
1145 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1147 // do not forget the DTP-lines!
1148 // there height depends on the font of the nearest character
1149 if (pit->params().lineTop())
1151 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1152 // and now the pagebreaks
1153 if (pit->params().pagebreakTop())
1154 maxasc += 3 * defaultRowHeight();
1156 if (pit->params().startOfAppendix())
1157 maxasc += 3 * defaultRowHeight();
1159 // This is special code for the chapter, since the label of this
1160 // layout is printed in an extra row
1161 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1162 && bv()->buffer()->params.secnumdepth >= 0)
1164 float spacing_val = 1.0;
1165 if (!pit->params().spacing().isDefault()) {
1166 spacing_val = pit->params().spacing().getValue();
1168 spacing_val = bv()->buffer()->params.spacing.getValue();
1171 labeladdon = int(font_metrics::maxDescent(labelfont) *
1172 layout->spacing.getValue() *
1174 + int(font_metrics::maxAscent(labelfont) *
1175 layout->spacing.getValue() *
1179 // special code for the top label
1180 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1181 || layout->labeltype == LABEL_BIBLIO
1182 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1183 && isFirstInSequence(pit, ownerParagraphs())
1184 && !pit->getLabelstring().empty())
1186 float spacing_val = 1.0;
1187 if (!pit->params().spacing().isDefault()) {
1188 spacing_val = pit->params().spacing().getValue();
1190 spacing_val = bv()->buffer()->params.spacing.getValue();
1194 (font_metrics::maxAscent(labelfont) +
1195 font_metrics::maxDescent(labelfont)) *
1196 layout->spacing.getValue() *
1198 + layout->topsep * defaultRowHeight()
1199 + layout->labelbottomsep * defaultRowHeight());
1202 // And now the layout spaces, for example before and after
1203 // a section, or between the items of a itemize or enumerate
1206 if (!pit->params().pagebreakTop()) {
1207 ParagraphList::iterator prev =
1208 depthHook(pit, ownerParagraphs(),
1210 if (prev != pit && prev->layout() == layout &&
1211 prev->getDepth() == pit->getDepth() &&
1212 prev->getLabelWidthString() == pit->getLabelWidthString())
1214 layoutasc = (layout->itemsep * defaultRowHeight());
1215 } else if (rit != firstRow()) {
1216 tmptop = layout->topsep;
1218 //if (boost::prior(pit)->getDepth() >= pit->getDepth())
1219 // tmptop -= getPar(previousRow(rit))->layout()->bottomsep;
1222 layoutasc = (tmptop * defaultRowHeight());
1223 } else if (pit->params().lineTop()) {
1224 tmptop = layout->topsep;
1227 layoutasc = (tmptop * defaultRowHeight());
1230 prev = outerHook(pit, ownerParagraphs());
1231 if (prev != ownerParagraphs().end()) {
1232 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1233 } else if (pit != ownerParagraphs().begin()) {
1234 ParagraphList::iterator prior_pit = boost::prior(pit);
1235 if (prior_pit->getDepth() != 0 ||
1236 prior_pit->layout() == layout) {
1237 maxasc += int(layout->parsep * defaultRowHeight());
1243 // is it a bottom line?
1244 if (boost::next(rit) == pit->rows.end()) {
1245 // the bottom margin
1246 ParagraphList::iterator nextpit = boost::next(pit);
1247 if (nextpit == ownerParagraphs().end() && !isInInset())
1248 maxdesc += PAPER_MARGIN;
1250 // add the vertical spaces, that the user added
1251 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1253 // do not forget the DTP-lines!
1254 // there height depends on the font of the nearest character
1255 if (pit->params().lineBottom())
1256 maxdesc += 2 * font_metrics::ascent('x',
1257 getFont(pit, max(pos_type(0), pit->size() - 1)));
1259 // and now the pagebreaks
1260 if (pit->params().pagebreakBottom())
1261 maxdesc += 3 * defaultRowHeight();
1263 // and now the layout spaces, for example before and after
1264 // a section, or between the items of a itemize or enumerate
1266 if (!pit->params().pagebreakBottom()
1267 && nextpit != ownerParagraphs().end()) {
1268 ParagraphList::iterator comparepit = pit;
1272 if (comparepit->getDepth() > nextpit->getDepth()) {
1273 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1274 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1275 if (comparepit->layout()!= nextpit->layout()
1276 || nextpit->getLabelWidthString() !=
1277 comparepit->getLabelWidthString())
1279 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1281 if (unusual > usual)
1282 layoutdesc = unusual;
1285 } else if (comparepit->getDepth() == nextpit->getDepth()) {
1287 if (comparepit->layout() != nextpit->layout()
1288 || nextpit->getLabelWidthString() !=
1289 comparepit->getLabelWidthString())
1290 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1295 // incalculate the layout spaces
1296 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1297 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1299 // calculate the new height of the text
1300 height -= rit->height();
1302 rit->height(maxasc + maxdesc + labeladdon);
1303 rit->baseline(maxasc + labeladdon);
1305 height += rit->height();
1307 rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1310 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1312 rit->width(maxwidth);
1314 prepareToPrint(pit, rit, x, dummy, dummy, dummy, false);
1316 rit->width(int(maxwidth + x));
1318 width = max(0, workWidth());
1319 RowList::iterator rit = firstRow();
1320 RowList::iterator end = endRow();
1321 ParagraphList::iterator it = ownerParagraphs().begin();
1322 while (rit != end) {
1323 if (rit->width() > width)
1324 width = rit->width();
1331 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1333 // allow only if at start or end, or all previous is new text
1334 if (cursor.pos() && cursor.pos() != cursor.par()->size()
1335 && cursor.par()->isChangeEdited(0, cursor.pos()))
1338 LyXTextClass const & tclass =
1339 bv()->buffer()->params.getLyXTextClass();
1340 LyXLayout_ptr const & layout = cursor.par()->layout();
1342 // this is only allowed, if the current paragraph is not empty or caption
1343 // and if it has not the keepempty flag active
1344 if (cursor.par()->empty() && !cursor.par()->allowEmpty()
1345 && layout->labeltype != LABEL_SENSITIVE)
1348 recordUndo(bv(), Undo::ATOMIC, cursor.par());
1350 // Always break behind a space
1352 // It is better to erase the space (Dekel)
1353 if (cursor.pos() < cursor.par()->size()
1354 && cursor.par()->isLineSeparator(cursor.pos()))
1355 cursor.par()->erase(cursor.pos());
1357 // break the paragraph
1361 keep_layout = layout->isEnvironment();
1363 // we need to set this before we insert the paragraph. IMO the
1364 // breakParagraph call should return a bool if it inserts the
1365 // paragraph before or behind and we should react on that one
1366 // but we can fix this in 1.3.0 (Jug 20020509)
1367 bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
1368 ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(),
1369 cursor.pos(), keep_layout);
1371 #warning Trouble Point! (Lgb)
1372 // When ::breakParagraph is called from within an inset we must
1373 // ensure that the correct ParagraphList is used. Today that is not
1374 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1375 ParagraphList::iterator next_par = boost::next(cursor.par());
1377 // well this is the caption hack since one caption is really enough
1378 if (layout->labeltype == LABEL_SENSITIVE) {
1380 // set to standard-layout
1381 cursor.par()->applyLayout(tclass.defaultLayout());
1383 // set to standard-layout
1384 next_par->applyLayout(tclass.defaultLayout());
1387 // if the cursor is at the beginning of a row without prior newline,
1389 // This touches only the screen-update. Otherwise we would may have
1390 // an empty row on the screen
1391 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1392 && !cursor.par()->isNewline(cursor.pos() - 1))
1397 while (!next_par->empty() && next_par->isNewline(0))
1401 redoParagraph(cursor.par());
1402 redoParagraph(next_par);
1404 // This check is necessary. Otherwise the new empty paragraph will
1405 // be deleted automatically. And it is more friendly for the user!
1406 if (cursor.pos() || isempty)
1407 setCursor(next_par, 0);
1409 setCursor(cursor.par(), 0);
1413 // convenience function
1414 void LyXText::redoParagraph()
1417 redoParagraph(cursor.par());
1418 setCursorIntern(cursor.par(), cursor.pos());
1422 // insert a character, moves all the following breaks in the
1423 // same Paragraph one to the right and make a rebreak
1424 void LyXText::insertChar(char c)
1426 recordUndo(bv(), Undo::INSERT, cursor.par());
1428 // When the free-spacing option is set for the current layout,
1429 // disable the double-space checking
1431 bool const freeSpacing = cursor.par()->layout()->free_spacing ||
1432 cursor.par()->isFreeSpacing();
1434 if (lyxrc.auto_number) {
1435 static string const number_operators = "+-/*";
1436 static string const number_unary_operators = "+-";
1437 static string const number_seperators = ".,:";
1439 if (current_font.number() == LyXFont::ON) {
1440 if (!IsDigit(c) && !contains(number_operators, c) &&
1441 !(contains(number_seperators, c) &&
1442 cursor.pos() >= 1 &&
1443 cursor.pos() < cursor.par()->size() &&
1444 getFont(cursor.par(), cursor.pos()).number() == LyXFont::ON &&
1445 getFont(cursor.par(), cursor.pos() - 1).number() == LyXFont::ON)
1447 number(bv()); // Set current_font.number to OFF
1448 } else if (IsDigit(c) &&
1449 real_current_font.isVisibleRightToLeft()) {
1450 number(bv()); // Set current_font.number to ON
1452 if (cursor.pos() > 0) {
1453 char const c = cursor.par()->getChar(cursor.pos() - 1);
1454 if (contains(number_unary_operators, c) &&
1455 (cursor.pos() == 1 ||
1456 cursor.par()->isSeparator(cursor.pos() - 2) ||
1457 cursor.par()->isNewline(cursor.pos() - 2))
1463 } else if (contains(number_seperators, c) &&
1464 cursor.pos() >= 2 &&
1467 cursor.pos() - 2).number() == LyXFont::ON) {
1478 // First check, if there will be two blanks together or a blank at
1479 // the beginning of a paragraph.
1480 // I decided to handle blanks like normal characters, the main
1481 // difference are the special checks when calculating the row.fill
1482 // (blank does not count at the end of a row) and the check here
1484 // The bug is triggered when we type in a description environment:
1485 // The current_font is not changed when we go from label to main text
1486 // and it should (along with realtmpfont) when we type the space.
1487 // CHECK There is a bug here! (Asger)
1489 // store the current font. This is because of the use of cursor
1490 // movements. The moving cursor would refresh the current font
1491 LyXFont realtmpfont = real_current_font;
1492 LyXFont rawtmpfont = current_font;
1494 if (!freeSpacing && IsLineSeparatorChar(c)) {
1495 if ((cursor.pos() > 0
1496 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1497 || (cursor.pos() > 0
1498 && cursor.par()->isNewline(cursor.pos() - 1))
1499 || (cursor.pos() == 0)) {
1500 static bool sent_space_message = false;
1501 if (!sent_space_message) {
1502 if (cursor.pos() == 0)
1503 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1505 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1506 sent_space_message = true;
1513 // Here case LyXText::InsertInset already inserted the character
1514 if (c != Paragraph::META_INSET)
1515 cursor.par()->insertChar(cursor.pos(), c);
1517 setCharFont(cursor.par(), cursor.pos(), rawtmpfont);
1519 current_font = rawtmpfont;
1520 real_current_font = realtmpfont;
1521 redoParagraph(cursor.par());
1522 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1528 void LyXText::charInserted()
1530 // Here we could call FinishUndo for every 20 characters inserted.
1531 // This is from my experience how emacs does it. (Lgb)
1532 static unsigned int counter;
1542 void LyXText::prepareToPrint(ParagraphList::iterator pit,
1543 RowList::iterator rit, double & x,
1544 double & fill_separator,
1545 double & fill_hfill,
1546 double & fill_label_hfill,
1549 double w = rit->fill();
1551 fill_label_hfill = 0;
1553 fill_label_hfill = 0;
1556 pit->isRightToLeftPar(bv()->buffer()->params);
1558 x = workWidth() > 0 ? rightMargin(pit, *bv()->buffer(), *rit) : 0;
1560 x = workWidth() > 0 ? leftMargin(pit, *rit) : 0;
1562 // is there a manual margin with a manual label
1563 LyXLayout_ptr const & layout = pit->layout();
1565 if (layout->margintype == MARGIN_MANUAL
1566 && layout->labeltype == LABEL_MANUAL) {
1567 /// We might have real hfills in the label part
1568 int nlh = numberOfLabelHfills(*pit, rit);
1570 // A manual label par (e.g. List) has an auto-hfill
1571 // between the label text and the body of the
1573 // But we don't want to do this auto hfill if the par
1578 if (nlh && !pit->getLabelWidthString().empty()) {
1579 fill_label_hfill = labelFill(pit, *rit) / double(nlh);
1583 // are there any hfills in the row?
1584 int const nh = numberOfHfills(*pit, rit);
1588 fill_hfill = w / nh;
1589 // we don't have to look at the alignment if it is ALIGN_LEFT and
1590 // if the row is already larger then the permitted width as then
1591 // we force the LEFT_ALIGN'edness!
1592 } else if (int(rit->width()) < workWidth()) {
1593 // is it block, flushleft or flushright?
1594 // set x how you need it
1596 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1597 align = layout->align;
1599 align = pit->params().align();
1602 // center displayed insets
1603 InsetOld * inset = 0;
1604 if (rit->pos() < pit->size()
1605 && pit->isInset(rit->pos())
1606 && (inset = pit->getInset(rit->pos()))
1607 && (inset->display())) // || (inset->scroll() < 0)))
1608 align = (inset->lyxCode() == InsetOld::MATHMACRO_CODE)
1609 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
1610 // ERT insets should always be LEFT ALIGNED on screen
1611 inset = pit->inInset();
1612 if (inset && inset->owner() &&
1613 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1615 align = LYX_ALIGN_LEFT;
1619 case LYX_ALIGN_BLOCK:
1621 int const ns = numberOfSeparators(*pit, rit);
1622 RowList::iterator next_row = boost::next(rit);
1624 && next_row != pit->rows.end()
1625 && !pit->isNewline(next_row->pos() - 1)
1626 && !(pit->isInset(next_row->pos())
1627 && pit->getInset(next_row->pos())
1628 && pit->getInset(next_row->pos())->display())
1630 fill_separator = w / ns;
1631 } else if (is_rtl) {
1636 case LYX_ALIGN_RIGHT:
1639 case LYX_ALIGN_CENTER:
1647 computeBidiTables(pit, bv()->buffer(), rit);
1649 pos_type body_pos = pit->beginningOfBody();
1650 pos_type last = lastPos(*pit, rit);
1653 (body_pos - 1 > last ||
1654 !pit->isLineSeparator(body_pos - 1))) {
1655 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1656 if (body_pos - 1 <= last)
1657 x += fill_label_hfill;
1663 // important for the screen
1666 // the cursor set functions have a special mechanism. When they
1667 // realize, that you left an empty paragraph, they will delete it.
1668 // They also delete the corresponding row
1670 void LyXText::cursorRightOneWord()
1672 ::cursorRightOneWord(cursor, ownerParagraphs());
1673 setCursor(cursor.par(), cursor.pos());
1677 // Skip initial whitespace at end of word and move cursor to *start*
1678 // of prior word, not to end of next prior word.
1679 void LyXText::cursorLeftOneWord()
1681 LyXCursor tmpcursor = cursor;
1682 ::cursorLeftOneWord(tmpcursor, ownerParagraphs());
1683 setCursor(tmpcursor.par(), tmpcursor.pos());
1687 void LyXText::selectWord(word_location loc)
1689 LyXCursor from = cursor;
1691 ::getWord(from, to, loc, ownerParagraphs());
1693 setCursor(from.par(), from.pos());
1696 selection.cursor = cursor;
1697 setCursor(to.par(), to.pos());
1702 // Select the word currently under the cursor when no
1703 // selection is currently set
1704 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1706 if (!selection.set()) {
1708 return selection.set();
1714 void LyXText::acceptChange()
1716 if (!selection.set() && cursor.par()->size())
1719 if (selection.start.par() == selection.end.par()) {
1720 LyXCursor & startc = selection.start;
1721 LyXCursor & endc = selection.end;
1722 recordUndo(bv(), Undo::INSERT, startc.par());
1723 startc.par()->acceptChange(startc.pos(), endc.pos());
1726 redoParagraph(startc.par());
1727 setCursorIntern(startc.par(), 0);
1729 #warning handle multi par selection
1733 void LyXText::rejectChange()
1735 if (!selection.set() && cursor.par()->size())
1738 if (selection.start.par() == selection.end.par()) {
1739 LyXCursor & startc = selection.start;
1740 LyXCursor & endc = selection.end;
1741 recordUndo(bv(), Undo::INSERT, startc.par());
1742 startc.par()->rejectChange(startc.pos(), endc.pos());
1745 redoParagraph(startc.par());
1746 setCursorIntern(startc.par(), 0);
1748 #warning handle multi par selection
1752 // This function is only used by the spellchecker for NextWord().
1753 // It doesn't handle LYX_ACCENTs and probably never will.
1755 LyXText::selectNextWordToSpellcheck(float & value)
1757 if (the_locking_inset) {
1758 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1759 if (!word.word().empty()) {
1760 value += float(cursor.y());
1761 value /= float(height);
1764 // we have to go on checking so move cursor to the next char
1765 if (cursor.pos() == cursor.par()->size()) {
1766 if (boost::next(cursor.par()) == ownerParagraphs().end())
1768 cursor.par(boost::next(cursor.par()));
1771 cursor.pos(cursor.pos() + 1);
1773 ParagraphList::iterator tmppit = cursor.par();
1775 // If this is not the very first word, skip rest of
1776 // current word because we are probably in the middle
1777 // of a word if there is text here.
1778 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
1779 while (cursor.pos() < cursor.par()->size()
1780 && cursor.par()->isLetter(cursor.pos()))
1781 cursor.pos(cursor.pos() + 1);
1784 // Now, skip until we have real text (will jump paragraphs)
1786 ParagraphList::iterator cpit = cursor.par();
1787 pos_type const cpos(cursor.pos());
1789 if (cpos == cpit->size()) {
1790 if (boost::next(cpit) != ownerParagraphs().end()) {
1791 cursor.par(boost::next(cpit));
1798 bool const is_good_inset = cpit->isInset(cpos)
1799 && cpit->getInset(cpos)->allowSpellcheck();
1801 if (!isDeletedText(*cpit, cpos)
1802 && (is_good_inset || cpit->isLetter(cpos)))
1805 cursor.pos(cpos + 1);
1808 // now check if we hit an inset so it has to be a inset containing text!
1809 if (cursor.pos() < cursor.par()->size() &&
1810 cursor.par()->isInset(cursor.pos())) {
1812 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1813 cursor.par()->getInset(cursor.pos())->localDispatch(cmd);
1814 // now call us again to do the above trick
1815 // but obviously we have to start from down below ;)
1816 return bv()->text->selectNextWordToSpellcheck(value);
1819 // Update the value if we changed paragraphs
1820 if (cursor.par() != tmppit) {
1821 setCursor(cursor.par(), cursor.pos());
1822 value = float(cursor.y())/float(height);
1825 // Start the selection from here
1826 selection.cursor = cursor;
1828 string lang_code = getFont(cursor.par(), cursor.pos()).language()->code();
1829 // and find the end of the word (insets like optional hyphens
1830 // and ligature break are part of a word)
1831 while (cursor.pos() < cursor.par()->size()
1832 && cursor.par()->isLetter(cursor.pos())
1833 && !isDeletedText(*cursor.par(), cursor.pos()))
1834 cursor.pos(cursor.pos() + 1);
1836 // Finally, we copy the word to a string and return it
1838 if (selection.cursor.pos() < cursor.pos()) {
1840 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1841 if (!cursor.par()->isInset(i))
1842 str += cursor.par()->getChar(i);
1845 return WordLangTuple(str, lang_code);
1849 // This one is also only for the spellchecker
1850 void LyXText::selectSelectedWord()
1852 if (the_locking_inset) {
1853 the_locking_inset->selectSelectedWord(bv());
1856 // move cursor to the beginning
1857 setCursor(selection.cursor.par(), selection.cursor.pos());
1859 // set the sel cursor
1860 selection.cursor = cursor;
1862 // now find the end of the word
1863 while (cursor.pos() < cursor.par()->size()
1864 && cursor.par()->isLetter(cursor.pos()))
1865 cursor.pos(cursor.pos() + 1);
1867 setCursor(cursor.par(), cursor.pos());
1869 // finally set the selection
1874 // Delete from cursor up to the end of the current or next word.
1875 void LyXText::deleteWordForward()
1877 if (cursor.par()->empty())
1880 LyXCursor tmpcursor = cursor;
1881 selection.set(true); // to avoid deletion
1882 cursorRightOneWord();
1883 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1884 selection.cursor = cursor;
1888 // Great, CutSelection() gets rid of multiple spaces.
1889 cutSelection(true, false);
1894 // Delete from cursor to start of current or prior word.
1895 void LyXText::deleteWordBackward()
1897 if (cursor.par()->empty())
1900 LyXCursor tmpcursor = cursor;
1901 selection.set(true); // to avoid deletion
1902 cursorLeftOneWord();
1903 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1904 selection.cursor = cursor;
1907 cutSelection(true, false);
1912 // Kill to end of line.
1913 void LyXText::deleteLineForward()
1915 if (cursor.par()->empty())
1916 // Paragraph is empty, so we just go to the right
1919 LyXCursor tmpcursor = cursor;
1920 // We can't store the row over a regular setCursor
1921 // so we set it to 0 and reset it afterwards.
1922 selection.set(true); // to avoid deletion
1924 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1925 selection.cursor = cursor;
1928 // What is this test for ??? (JMarc)
1929 if (!selection.set()) {
1930 deleteWordForward();
1932 cutSelection(true, false);
1938 void LyXText::changeCase(LyXText::TextCase action)
1943 if (selection.set()) {
1944 from = selection.start;
1948 ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1949 setCursor(to.par(), to.pos() + 1);
1952 recordUndo(bv(), Undo::ATOMIC, from.par(), to.par());
1954 pos_type pos = from.pos();
1955 ParagraphList::iterator pit = from.par();
1957 while (pit != ownerParagraphs().end() &&
1958 (pos != to.pos() || pit != to.par())) {
1959 if (pos == pit->size()) {
1964 unsigned char c = pit->getChar(pos);
1965 if (!IsInsetChar(c)) {
1967 case text_lowercase:
1970 case text_capitalization:
1972 action = text_lowercase;
1974 case text_uppercase:
1980 pit->setChar(pos, c);
1986 void LyXText::Delete()
1988 // this is a very easy implementation
1990 LyXCursor old_cursor = cursor;
1991 int const old_cur_par_id = old_cursor.par()->id();
1992 int const old_cur_par_prev_id =
1993 (old_cursor.par() != ownerParagraphs().begin() ?
1994 boost::prior(old_cursor.par())->id() : -1);
1996 // just move to the right
1999 // CHECK Look at the comment here.
2000 // This check is not very good...
2001 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2002 // and that can very well delete the par or par->previous in
2003 // old_cursor. Will a solution where we compare paragraph id's
2005 if ((cursor.par() != ownerParagraphs().begin() ? boost::prior(cursor.par())->id() : -1)
2006 == old_cur_par_prev_id
2007 && cursor.par()->id() != old_cur_par_id) {
2008 // delete-empty-paragraph-mechanism has done it
2012 // if you had success make a backspace
2013 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2014 LyXCursor tmpcursor = cursor;
2015 // to make sure undo gets the right cursor position
2016 cursor = old_cursor;
2017 recordUndo(bv(), Undo::DELETE, cursor.par());
2024 void LyXText::backspace()
2026 // Get the font that is used to calculate the baselineskip
2027 pos_type lastpos = cursor.par()->size();
2029 if (cursor.pos() == 0) {
2030 // The cursor is at the beginning of a paragraph,
2031 // so the the backspace will collapse two paragraphs into one.
2033 // but it's not allowed unless it's new
2034 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
2037 // we may paste some paragraphs
2039 // is it an empty paragraph?
2042 || (lastpos == 1 && cursor.par()->isSeparator(0))) {
2043 // This is an empty paragraph and we delete it just
2044 // by moving the cursor one step
2045 // left and let the DeleteEmptyParagraphMechanism
2046 // handle the actual deletion of the paragraph.
2048 if (cursor.par() != ownerParagraphs().begin()) {
2049 ParagraphList::iterator tmppit = boost::prior(cursor.par());
2050 if (cursor.par()->layout() == tmppit->layout()
2051 && cursor.par()->getAlign() == tmppit->getAlign()) {
2052 // Inherit bottom DTD from the paragraph below.
2053 // (the one we are deleting)
2054 tmppit->params().lineBottom(cursor.par()->params().lineBottom());
2055 tmppit->params().spaceBottom(cursor.par()->params().spaceBottom());
2056 tmppit->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2061 // the layout things can change the height of a row !
2062 setHeightOfRow(cursor.par(), cursorRow());
2067 if (cursor.par() != ownerParagraphs().begin()) {
2068 recordUndo(bv(), Undo::DELETE,
2069 boost::prior(cursor.par()),
2073 ParagraphList::iterator tmppit = cursor.par();
2074 // We used to do cursorLeftIntern() here, but it is
2075 // not a good idea since it triggers the auto-delete
2076 // mechanism. So we do a cursorLeftIntern()-lite,
2077 // without the dreaded mechanism. (JMarc)
2078 if (cursor.par() != ownerParagraphs().begin()) {
2079 // steps into the above paragraph.
2080 setCursorIntern(boost::prior(cursor.par()),
2081 boost::prior(cursor.par())->size(),
2085 // Pasting is not allowed, if the paragraphs have different
2086 // layout. I think it is a real bug of all other
2087 // word processors to allow it. It confuses the user.
2088 //Correction: Pasting is always allowed with standard-layout
2089 LyXTextClass const & tclass =
2090 bv()->buffer()->params.getLyXTextClass();
2092 if (cursor.par() != tmppit
2093 && (cursor.par()->layout() == tmppit->layout()
2094 || tmppit->layout() == tclass.defaultLayout())
2095 && cursor.par()->getAlign() == tmppit->getAlign()) {
2096 mergeParagraph(bv()->buffer()->params,
2097 bv()->buffer()->paragraphs, cursor.par());
2099 if (cursor.pos() && cursor.par()->isSeparator(cursor.pos() - 1))
2100 cursor.pos(cursor.pos() - 1);
2102 // the row may have changed, block, hfills etc.
2104 setCursor(cursor.par(), cursor.pos(), false);
2107 // this is the code for a normal backspace, not pasting
2109 recordUndo(bv(), Undo::DELETE, cursor.par());
2110 // We used to do cursorLeftIntern() here, but it is
2111 // not a good idea since it triggers the auto-delete
2112 // mechanism. So we do a cursorLeftIntern()-lite,
2113 // without the dreaded mechanism. (JMarc)
2114 setCursorIntern(cursor.par(), cursor.pos() - 1,
2115 false, cursor.boundary());
2116 cursor.par()->erase(cursor.pos());
2119 lastpos = cursor.par()->size();
2120 if (cursor.pos() == lastpos)
2124 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
2128 RowList::iterator LyXText::cursorRow() const
2130 return getRow(cursor.par(), cursor.pos());
2134 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
2136 return getRow(cur.par(), cur.pos());
2141 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
2143 RowList::iterator rit = pit->rows.begin();
2144 RowList::iterator end = pit->rows.end();
2146 #warning Why is this next thing needed? (Andre)
2149 && boost::next(rit) != end
2150 && boost::next(rit)->pos() <= pos)
2157 // returns pointer to a specified row
2159 LyXText::getRow(ParagraphList::iterator pit, pos_type pos, int & y) const
2166 ParagraphList::iterator it = ownerParagraphs().begin();
2167 for ( ; it != pit; ++it) {
2168 RowList::iterator beg = it->rows.begin();
2169 RowList::iterator end = it->rows.end();
2170 for (RowList::iterator rit = beg; rit != end; ++rit)
2174 RowList::iterator rit = pit->rows.begin();
2175 RowList::iterator end = pit->rows.end();
2178 && boost::next(rit) != end
2179 && boost::next(rit)->pos() <= pos) {
2188 // returns pointer to some fancy row 'below' specified row
2189 RowList::iterator LyXText::cursorIRow() const
2192 return getRow(cursor.par(), cursor.pos(), y);
2196 RowList::iterator LyXText::getRowNearY(int & y,
2197 ParagraphList::iterator & pit) const
2199 ParagraphList::iterator const end = ownerParagraphs().end();
2200 //lyxerr << "getRowNearY: y " << y << endl;
2202 for (pit = ownerParagraphs().begin(); pit != end; ++pit) {
2203 RowList::iterator rit = pit->rows.begin();
2204 RowList::iterator rend = pit->rows.end();
2206 for ( ; rit != rend; ++rit) {
2208 if (rit->y() >= y) {
2209 if (rit != firstRow())
2210 previousRow(pit, rit);
2218 pit = ownerParagraphs().begin();
2220 lyxerr << "row not found near " << y << " pit: " << &*pit << endl;
2223 pit = boost::prior(ownerParagraphs().end());
2224 lyxerr << "row not found near " << y << " pit: " << &*pit << endl;
2230 int LyXText::getDepth() const
2232 return cursor.par()->getDepth();
2236 RowList::iterator LyXText::firstRow() const
2238 return ownerParagraphs().front().rows.begin();
2242 RowList::iterator LyXText::lastRow() const
2244 return boost::prior(endRow());
2248 RowList::iterator LyXText::endRow() const
2250 return ownerParagraphs().back().rows.end();
2254 void LyXText::nextRow(ParagraphList::iterator & pit,
2255 RowList::iterator & rit) const
2257 if (boost::next(rit) != pit->rows.end()) {
2258 rit = boost::next(rit);
2261 if (pit == ownerParagraphs().end())
2262 rit = boost::next(rit);
2264 rit = pit->rows.begin();
2269 void LyXText::previousRow(ParagraphList::iterator & pit,
2270 RowList::iterator & rit) const
2272 if (rit != pit->rows.begin())
2273 rit = boost::prior(rit);
2275 Assert(pit != ownerParagraphs().begin());
2277 rit = boost::prior(pit->rows.end());
2282 bool LyXText::noRows() const
2284 return ownerParagraphs().begin()->rows.empty();