3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * \author Jürgen Vigna
14 * Full author contact details are available in file CREDITS.
23 #include "buffer_funcs.h"
24 #include "bufferparams.h"
25 #include "BufferView.h"
27 #include "coordcache.h"
28 #include "CutAndPaste.h"
30 #include "dispatchresult.h"
32 #include "errorlist.h"
33 #include "funcrequest.h"
35 #include "FontIterator.h"
39 #include "lyxlength.h"
43 #include "lyxrow_funcs.h"
44 #include "metricsinfo.h"
45 #include "paragraph.h"
46 #include "paragraph_funcs.h"
47 #include "ParagraphParameters.h"
48 #include "rowpainter.h"
51 #include "WordLangTuple.h"
53 #include "frontends/font_metrics.h"
54 #include "frontends/LyXView.h"
55 #include "frontends/Painter.h"
57 #include "insets/insettext.h"
58 #include "insets/insetbibitem.h"
59 #include "insets/insethfill.h"
60 #include "insets/insetlatexaccent.h"
61 #include "insets/insetline.h"
62 #include "insets/insetnewline.h"
63 #include "insets/insetpagebreak.h"
64 #include "insets/insetoptarg.h"
65 #include "insets/insetspace.h"
66 #include "insets/insetspecialchar.h"
67 #include "insets/insettabular.h"
69 #include "support/lstrings.h"
70 #include "support/textutils.h"
71 #include "support/convert.h"
77 using lyx::word_location;
79 using lyx::support::bformat;
80 using lyx::support::contains;
81 using lyx::support::lowercase;
82 using lyx::support::split;
83 using lyx::support::uppercase;
85 using lyx::cap::cutSelection;
98 int numberOfSeparators(Paragraph const & par, Row const & row)
100 pos_type const first = max(row.pos(), par.beginOfBody());
101 pos_type const last = row.endpos() - 1;
103 for (pos_type p = first; p < last; ++p) {
104 if (par.isSeparator(p))
111 int numberOfLabelHfills(Paragraph const & par, Row const & row)
113 pos_type last = row.endpos() - 1;
114 pos_type first = row.pos();
116 // hfill *DO* count at the beginning of paragraphs!
118 while (first < last && par.isHfill(first))
122 last = min(last, par.beginOfBody());
124 for (pos_type p = first; p < last; ++p) {
132 int numberOfHfills(Paragraph const & par, Row const & row)
134 pos_type const last = row.endpos() - 1;
135 pos_type first = row.pos();
137 // hfill *DO* count at the beginning of paragraphs!
139 while (first < last && par.isHfill(first))
143 first = max(first, par.beginOfBody());
146 for (pos_type p = first; p < last; ++p) {
154 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
155 string const & token, LyXFont & font)
157 static Change change;
159 BufferParams const & bp = buf.params();
161 if (token[0] != '\\') {
162 string::const_iterator cit = token.begin();
163 for (; cit != token.end(); ++cit) {
164 par.insertChar(par.size(), (*cit), font, change);
166 } else if (token == "\\begin_layout") {
168 string layoutname = lex.getString();
170 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
173 LyXTextClass const & tclass = bp.getLyXTextClass();
175 if (layoutname.empty()) {
176 layoutname = tclass.defaultLayoutName();
179 bool hasLayout = tclass.hasLayout(layoutname);
182 buf.error(ErrorItem(_("Unknown layout"),
183 bformat(_("Layout '%1$s' does not exists in textclass '%2$s'\nTrying to use the default instead.\n"),
184 layoutname, tclass.name()), par.id(), 0, par.size()));
185 layoutname = tclass.defaultLayoutName();
188 par.layout(bp.getLyXTextClass()[layoutname]);
190 // Test whether the layout is obsolete.
191 LyXLayout_ptr const & layout = par.layout();
192 if (!layout->obsoleted_by().empty())
193 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
195 par.params().read(lex);
197 } else if (token == "\\end_layout") {
198 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
199 << "Missing \\begin_layout?.\n";
200 } else if (token == "\\end_inset") {
201 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
202 << "Missing \\begin_inset?.\n";
203 } else if (token == "\\begin_inset") {
204 InsetBase * inset = readInset(lex, buf);
206 par.insertInset(par.size(), inset, font, change);
209 string line = lex.getString();
210 buf.error(ErrorItem(_("Unknown Inset"), line,
211 par.id(), 0, par.size()));
213 } else if (token == "\\family") {
215 font.setLyXFamily(lex.getString());
216 } else if (token == "\\series") {
218 font.setLyXSeries(lex.getString());
219 } else if (token == "\\shape") {
221 font.setLyXShape(lex.getString());
222 } else if (token == "\\size") {
224 font.setLyXSize(lex.getString());
225 } else if (token == "\\lang") {
227 string const tok = lex.getString();
228 Language const * lang = languages.getLanguage(tok);
230 font.setLanguage(lang);
232 font.setLanguage(bp.language);
233 lex.printError("Unknown language `$$Token'");
235 } else if (token == "\\numeric") {
237 font.setNumber(font.setLyXMisc(lex.getString()));
238 } else if (token == "\\emph") {
240 font.setEmph(font.setLyXMisc(lex.getString()));
241 } else if (token == "\\bar") {
243 string const tok = lex.getString();
246 font.setUnderbar(LyXFont::ON);
247 else if (tok == "no")
248 font.setUnderbar(LyXFont::OFF);
249 else if (tok == "default")
250 font.setUnderbar(LyXFont::INHERIT);
252 lex.printError("Unknown bar font flag "
254 } else if (token == "\\noun") {
256 font.setNoun(font.setLyXMisc(lex.getString()));
257 } else if (token == "\\color") {
259 font.setLyXColor(lex.getString());
260 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
262 // Insets don't make sense in a free-spacing context! ---Kayvan
263 if (par.isFreeSpacing()) {
264 if (token == "\\InsetSpace")
265 par.insertChar(par.size(), ' ', font, change);
266 else if (lex.isOK()) {
268 string const next_token = lex.getString();
269 if (next_token == "\\-")
270 par.insertChar(par.size(), '-', font, change);
272 lex.printError("Token `$$Token' "
274 "paragraph layout!");
278 auto_ptr<InsetBase> inset;
279 if (token == "\\SpecialChar" )
280 inset.reset(new InsetSpecialChar);
282 inset.reset(new InsetSpace);
283 inset->read(buf, lex);
284 par.insertInset(par.size(), inset.release(),
287 } else if (token == "\\i") {
288 auto_ptr<InsetBase> inset(new InsetLatexAccent);
289 inset->read(buf, lex);
290 par.insertInset(par.size(), inset.release(), font, change);
291 } else if (token == "\\backslash") {
292 par.insertChar(par.size(), '\\', font, change);
293 } else if (token == "\\newline") {
294 auto_ptr<InsetBase> inset(new InsetNewline);
295 inset->read(buf, lex);
296 par.insertInset(par.size(), inset.release(), font, change);
297 } else if (token == "\\LyXTable") {
298 auto_ptr<InsetBase> inset(new InsetTabular(buf));
299 inset->read(buf, lex);
300 par.insertInset(par.size(), inset.release(), font, change);
301 } else if (token == "\\bibitem") {
302 InsetCommandParams p("bibitem", "dummy");
303 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
304 inset->read(buf, lex);
305 par.insertInset(par.size(), inset.release(), font, change);
306 } else if (token == "\\hfill") {
307 par.insertInset(par.size(), new InsetHFill, font, change);
308 } else if (token == "\\lyxline") {
309 par.insertInset(par.size(), new InsetLine, font, change);
310 } else if (token == "\\newpage") {
311 par.insertInset(par.size(), new InsetPagebreak, font, change);
312 } else if (token == "\\change_unchanged") {
313 // Hack ! Needed for empty paragraphs :/
314 // FIXME: is it still ??
317 change = Change(Change::UNCHANGED);
318 } else if (token == "\\change_inserted") {
320 std::istringstream is(lex.getString());
324 change = Change(Change::INSERTED, bp.author_map[aid], ct);
325 } else if (token == "\\change_deleted") {
327 std::istringstream is(lex.getString());
331 change = Change(Change::DELETED, bp.author_map[aid], ct);
334 buf.error(ErrorItem(_("Unknown token"),
335 bformat(_("Unknown token: %1$s %2$s\n"), token, lex.getString()),
336 par.id(), 0, par.size()));
341 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex)
344 string token = lex.getString();
349 readParToken(buf, par, lex, token, font);
352 token = lex.getString();
357 if (token == "\\end_layout") {
358 //Ok, paragraph finished
362 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
363 << token << '\'' << endl;
364 if (token == "\\begin_layout" || token == "\\end_document"
365 || token == "\\end_inset" || token == "\\begin_deeper"
366 || token == "\\end_deeper") {
367 lex.pushToken(token);
368 lyxerr << "Paragraph ended in line "
369 << lex.getLineNo() << "\n"
370 << "Missing \\end_layout.\n";
381 BufferView * LyXText::bv() const
383 BOOST_ASSERT(bv_owner != 0);
388 double LyXText::spacing(Paragraph const & par) const
390 if (par.params().spacing().isDefault())
391 return bv()->buffer()->params().spacing().getValue();
392 return par.params().spacing().getValue();
396 int LyXText::width() const
402 int LyXText::height() const
404 return dim_.height();
408 int LyXText::singleWidth(Paragraph const & par, pos_type pos) const
410 return singleWidth(par, pos, par.getChar(pos), getFont(par, pos));
414 int LyXText::singleWidth(Paragraph const & par,
415 pos_type pos, char c, LyXFont const & font) const
417 BOOST_ASSERT(pos < par.size());
419 // The most common case is handled first (Asger)
420 if (IsPrintable(c)) {
421 if (font.language()->RightToLeft()) {
422 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
423 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
424 && font.language()->lang() == "arabic") {
425 if (Encodings::IsComposeChar_arabic(c))
427 c = par.transformChar(c, pos);
428 } else if (font.language()->lang() == "hebrew" &&
429 Encodings::IsComposeChar_hebrew(c))
432 return font_metrics::width(c, font);
435 if (c == Paragraph::META_INSET)
436 return par.getInset(pos)->width();
438 if (IsSeparatorChar(c))
440 return font_metrics::width(c, font);
444 int LyXText::leftMargin(pit_type pit) const
446 BOOST_ASSERT(pit >= 0);
447 BOOST_ASSERT(pit < int(pars_.size()));
448 return leftMargin(pit, pars_[pit].size());
452 int LyXText::leftMargin(pit_type const pit, pos_type const pos) const
454 BOOST_ASSERT(pit >= 0);
455 BOOST_ASSERT(pit < int(pars_.size()));
456 Paragraph const & par = pars_[pit];
457 BOOST_ASSERT(pos >= 0);
458 BOOST_ASSERT(pos <= par.size());
459 //lyxerr << "LyXText::leftMargin: pit: " << pit << " pos: " << pos << endl;
460 LyXTextClass const & tclass =
461 bv()->buffer()->params().getLyXTextClass();
462 LyXLayout_ptr const & layout = par.layout();
464 string parindent = layout->parindent;
469 l_margin += changebarMargin();
471 l_margin += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
473 if (par.getDepth() != 0) {
474 // find the next level paragraph
475 pit_type newpar = outerHook(pit, pars_);
476 if (newpar != pit_type(pars_.size())) {
477 if (pars_[newpar].layout()->isEnvironment()) {
478 l_margin = leftMargin(newpar);
480 if (par.layout() == tclass.defaultLayout()) {
481 if (pars_[newpar].params().noindent())
484 parindent = pars_[newpar].layout()->parindent;
489 LyXFont const labelfont = getLabelFont(par);
490 switch (layout->margintype) {
492 if (!layout->leftmargin.empty())
493 l_margin += font_metrics::signedWidth(layout->leftmargin,
494 tclass.defaultfont());
495 if (!par.getLabelstring().empty()) {
496 l_margin += font_metrics::signedWidth(layout->labelindent,
498 l_margin += font_metrics::width(par.getLabelstring(),
500 l_margin += font_metrics::width(layout->labelsep, labelfont);
505 l_margin += font_metrics::signedWidth(layout->labelindent, labelfont);
506 // The width of an empty par, even with manual label, should be 0
507 if (!par.empty() && pos >= par.beginOfBody()) {
508 if (!par.getLabelWidthString().empty()) {
509 l_margin += font_metrics::width(par.getLabelWidthString(),
511 l_margin += font_metrics::width(layout->labelsep, labelfont);
517 l_margin += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
518 / (par.getDepth() + 4);
521 case MARGIN_FIRST_DYNAMIC:
522 if (layout->labeltype == LABEL_MANUAL) {
523 if (pos >= par.beginOfBody()) {
524 l_margin += font_metrics::signedWidth(layout->leftmargin,
527 l_margin += font_metrics::signedWidth(layout->labelindent,
531 // Special case to fix problems with
533 || (layout->labeltype == LABEL_STATIC
534 && layout->latextype == LATEX_ENVIRONMENT
535 && !isFirstInSequence(pit, pars_))) {
536 l_margin += font_metrics::signedWidth(layout->leftmargin,
538 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
539 && layout->labeltype != LABEL_BIBLIO
540 && layout->labeltype !=
541 LABEL_CENTERED_TOP_ENVIRONMENT) {
542 l_margin += font_metrics::signedWidth(layout->labelindent,
544 l_margin += font_metrics::width(layout->labelsep, labelfont);
545 l_margin += font_metrics::width(par.getLabelstring(),
550 case MARGIN_RIGHT_ADDRESS_BOX: {
552 // ok, a terrible hack. The left margin depends on the widest
553 // row in this paragraph.
554 RowList::iterator rit = par.rows().begin();
555 RowList::iterator end = par.rows().end();
557 #warning This is wrong.
559 int minfill = maxwidth_;
560 for ( ; rit != end; ++rit)
561 if (rit->fill() < minfill)
562 minfill = rit->fill();
563 l_margin += font_metrics::signedWidth(layout->leftmargin,
564 tclass.defaultfont());
567 // also wrong, but much shorter.
568 l_margin += maxwidth_ / 2;
573 if (!par.params().leftIndent().zero())
574 l_margin += par.params().leftIndent().inPixels(maxwidth_);
578 if (par.params().align() == LYX_ALIGN_LAYOUT)
579 align = layout->align;
581 align = par.params().align();
583 // set the correct parindent
585 && (layout->labeltype == LABEL_NO_LABEL
586 || layout->labeltype == LABEL_TOP_ENVIRONMENT
587 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
588 || (layout->labeltype == LABEL_STATIC
589 && layout->latextype == LATEX_ENVIRONMENT
590 && !isFirstInSequence(pit, pars_)))
591 && align == LYX_ALIGN_BLOCK
592 && !par.params().noindent()
593 // in charstyles, tabulars and ert paragraphs are never indented!
594 && ((par.ownerCode() != InsetBase::TEXT_CODE || isMainText())
595 && par.ownerCode() != InsetBase::ERT_CODE
596 && par.ownerCode() != InsetBase::CHARSTYLE_CODE)
597 && (par.layout() != tclass.defaultLayout()
598 || bv()->buffer()->params().paragraph_separation ==
599 BufferParams::PARSEP_INDENT))
601 l_margin += font_metrics::signedWidth(parindent, tclass.defaultfont());
608 int LyXText::rightMargin(Paragraph const & par) const
610 // We do not want rightmargins on inner texts.
611 if (bv()->text() != this)
614 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
617 + font_metrics::signedWidth(tclass.rightmargin(),
618 tclass.defaultfont())
619 + font_metrics::signedWidth(par.layout()->rightmargin,
620 tclass.defaultfont())
621 * 4 / (par.getDepth() + 4);
627 int LyXText::labelEnd(pit_type const pit) const
629 // labelEnd is only needed if the layout fills a flushleft label.
630 if (pars_[pit].layout()->margintype != MARGIN_MANUAL)
632 // return the beginning of the body
633 return leftMargin(pit);
639 // this needs special handling - only newlines count as a break point
640 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
642 pos_type const end = par.size();
645 if (par.isNewline(i))
654 void LyXText::rowBreakPoint(pit_type const pit, Row & row) const
656 Paragraph const & par = pars_[pit];
657 pos_type const end = par.size();
658 pos_type const pos = row.pos();
664 // maximum pixel width of a row
665 int width = maxwidth_ - rightMargin(par); // - leftMargin(pit, row);
671 LyXLayout_ptr const & layout = par.layout();
673 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
674 row.endpos(addressBreakPoint(pos, par));
678 pos_type const body_pos = par.beginOfBody();
681 // Now we iterate through until we reach the right margin
682 // or the end of the par, then choose the possible break
685 int const left = leftMargin(pit, pos);
688 // pixel width since last breakpoint
691 FontIterator fi = FontIterator(*this, par, pos);
692 pos_type point = end;
694 for ( ; i < end; ++i, ++fi) {
695 char const c = par.getChar(i);
698 int thiswidth = singleWidth(par, i, c, *fi);
700 // add the auto-hfill from label end to the body
701 if (body_pos && i == body_pos) {
702 int add = font_metrics::width(layout->labelsep, getLabelFont(par));
703 if (par.isLineSeparator(i - 1))
704 add -= singleWidth(par, i - 1);
706 add = std::max(add, labelEnd(pit) - x);
711 chunkwidth += thiswidth;
714 // break before a character that will fall off
715 // the right of the row
717 // if no break before, break here
718 if (point == end || chunkwidth >= width - left) {
725 // exit on last registered breakpoint:
729 if (par.isNewline(i)) {
735 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
740 if (par.isInset(i) && par.getInset(i)->display()) {
746 if (!par.isInset(i) || par.getInset(i)->isChar()) {
747 // some insets are line separators too
748 if (par.isLineSeparator(i)) {
749 // register breakpoint:
756 // maybe found one, but the par is short enough.
757 if (i == end && x < width)
760 // manual labels cannot be broken in LaTeX. But we
761 // want to make our on-screen rendering of footnotes
763 if (body_pos && point < body_pos)
770 void LyXText::setRowWidth(pit_type const pit, Row & row) const
772 // get the pure distance
773 pos_type const end = row.endpos();
775 Paragraph const & par = pars_[pit];
776 string labelsep = par.layout()->labelsep;
777 int w = leftMargin(pit, row.pos());
779 pos_type const body_pos = par.beginOfBody();
780 pos_type i = row.pos();
783 FontIterator fi = FontIterator(*this, par, i);
784 for ( ; i < end; ++i, ++fi) {
785 if (body_pos > 0 && i == body_pos) {
786 w += font_metrics::width(labelsep, getLabelFont(par));
787 if (par.isLineSeparator(i - 1))
788 w -= singleWidth(par, i - 1);
789 w = max(w, labelEnd(pit));
791 char const c = par.getChar(i);
792 w += singleWidth(par, i, c, *fi);
796 if (body_pos > 0 && body_pos >= end) {
797 w += font_metrics::width(labelsep, getLabelFont(par));
798 if (end > 0 && par.isLineSeparator(end - 1))
799 w -= singleWidth(par, end - 1);
800 w = max(w, labelEnd(pit));
803 row.width(w + rightMargin(par));
807 // returns the minimum space a manual label needs on the screen in pixel
808 int LyXText::labelFill(Paragraph const & par, Row const & row) const
810 pos_type last = par.beginOfBody();
812 BOOST_ASSERT(last > 0);
814 // -1 because a label ends with a space that is in the label
817 // a separator at this end does not count
818 if (par.isLineSeparator(last))
822 for (pos_type i = row.pos(); i <= last; ++i)
823 w += singleWidth(par, i);
825 string const & label = par.params().labelWidthString();
829 return max(0, font_metrics::width(label, getLabelFont(par)) - w);
833 LColor_color LyXText::backgroundColor() const
835 return LColor_color(LColor::color(background_color_));
839 void LyXText::setHeightOfRow(pit_type const pit, Row & row)
841 Paragraph const & par = pars_[pit];
842 // get the maximum ascent and the maximum descent
843 double layoutasc = 0;
844 double layoutdesc = 0;
845 double const dh = defaultRowHeight();
847 // ok, let us initialize the maxasc and maxdesc value.
848 // Only the fontsize count. The other properties
849 // are taken from the layoutfont. Nicer on the screen :)
850 LyXLayout_ptr const & layout = par.layout();
852 // as max get the first character of this row then it can
853 // increase but not decrease the height. Just some point to
854 // start with so we don't have to do the assignment below too
856 LyXFont font = getFont(par, row.pos());
857 LyXFont::FONT_SIZE const tmpsize = font.size();
858 font = getLayoutFont(pit);
859 LyXFont::FONT_SIZE const size = font.size();
860 font.setSize(tmpsize);
862 LyXFont labelfont = getLabelFont(par);
864 // these are minimum values
865 double const spacing_val = layout->spacing.getValue() * spacing(par);
866 //lyxerr << "spacing_val = " << spacing_val << endl;
867 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
868 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
870 // insets may be taller
871 InsetList::const_iterator ii = par.insetlist.begin();
872 InsetList::const_iterator iend = par.insetlist.end();
873 for ( ; ii != iend; ++ii) {
874 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
875 maxasc = max(maxasc, ii->inset->ascent());
876 maxdesc = max(maxdesc, ii->inset->descent());
880 // Check if any custom fonts are larger (Asger)
881 // This is not completely correct, but we can live with the small,
882 // cosmetic error for now.
884 pos_type const pos_end = row.endpos();
886 LyXFont::FONT_SIZE maxsize =
887 par.highestFontInRange(row.pos(), pos_end, size);
888 if (maxsize > font.size()) {
889 font.setSize(maxsize);
890 maxasc = max(maxasc, font_metrics::maxAscent(font));
891 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
894 // This is nicer with box insets:
901 if (row.pos() == 0) {
902 BufferParams const & bufparams = bv()->buffer()->params();
903 // some parksips VERY EASY IMPLEMENTATION
904 if (bv()->buffer()->params().paragraph_separation
905 == BufferParams::PARSEP_SKIP
907 && ((layout->isParagraph() && par.getDepth() == 0)
908 || (pars_[pit - 1].layout()->isParagraph()
909 && pars_[pit - 1].getDepth() == 0)))
911 maxasc += bufparams.getDefSkip().inPixels(*bv());
914 if (par.params().startOfAppendix())
915 maxasc += int(3 * dh);
917 // This is special code for the chapter, since the label of this
918 // layout is printed in an extra row
919 if (layout->counter == "chapter"
920 && !par.params().labelString().empty()) {
921 labeladdon = int(font_metrics::maxHeight(labelfont)
922 * layout->spacing.getValue()
926 // special code for the top label
927 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
928 || layout->labeltype == LABEL_BIBLIO
929 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
930 && isFirstInSequence(pit, paragraphs())
931 && !par.getLabelstring().empty())
934 font_metrics::maxHeight(labelfont)
935 * layout->spacing.getValue()
937 + (layout->topsep + layout->labelbottomsep) * dh);
940 // Add the layout spaces, for example before and after
941 // a section, or between the items of a itemize or enumerate
944 pit_type prev = depthHook(pit, pars_, par.getDepth());
946 && pars_[prev].layout() == layout
947 && pars_[prev].getDepth() == par.getDepth()
948 && pars_[prev].getLabelWidthString() == par.getLabelWidthString())
950 layoutasc = layout->itemsep * dh;
951 } else if (pit != 0 || row.pos() != 0) {
952 if (layout->topsep > 0)
953 layoutasc = layout->topsep * dh;
956 prev = outerHook(pit, pars_);
957 if (prev != pit_type(pars_.size())) {
958 maxasc += int(pars_[prev].layout()->parsep * dh);
959 } else if (pit != 0) {
960 if (pars_[pit - 1].getDepth() != 0 ||
961 pars_[pit - 1].layout() == layout) {
962 maxasc += int(layout->parsep * dh);
967 // is it a bottom line?
968 if (row.endpos() >= par.size()) {
969 // add the layout spaces, for example before and after
970 // a section, or between the items of a itemize or enumerate
972 pit_type nextpit = pit + 1;
973 if (nextpit != pit_type(pars_.size())) {
978 if (pars_[cpit].getDepth() > pars_[nextpit].getDepth()) {
979 usual = pars_[cpit].layout()->bottomsep * dh;
980 cpit = depthHook(cpit, paragraphs(), pars_[nextpit].getDepth());
981 if (pars_[cpit].layout() != pars_[nextpit].layout()
982 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
984 unusual = pars_[cpit].layout()->bottomsep * dh;
986 layoutdesc = max(unusual, usual);
987 } else if (pars_[cpit].getDepth() == pars_[nextpit].getDepth()) {
988 if (pars_[cpit].layout() != pars_[nextpit].layout()
989 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
990 layoutdesc = int(pars_[cpit].layout()->bottomsep * dh);
995 // incalculate the layout spaces
996 maxasc += int(layoutasc * 2 / (2 + pars_[pit].getDepth()));
997 maxdesc += int(layoutdesc * 2 / (2 + pars_[pit].getDepth()));
999 // Top and bottom margin of the document (only at top-level)
1000 if (bv_owner->text() == this) {
1001 if (pit == 0 && row.pos() == 0)
1003 if (pit == pars_.size() - 1 && row.endpos() == par.size())
1007 row.ascent(maxasc + labeladdon);
1008 row.descent(maxdesc);
1016 void LyXText::breakParagraph(LCursor & cur, bool keep_layout)
1018 BOOST_ASSERT(this == cur.text());
1019 // allow only if at start or end, or all previous is new text
1020 Paragraph & cpar = cur.paragraph();
1021 pit_type cpit = cur.pit();
1023 if (cur.pos() != 0 && cur.pos() != cur.lastpos()
1024 && cpar.isChangeEdited(0, cur.pos()))
1027 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
1028 LyXLayout_ptr const & layout = cpar.layout();
1030 // this is only allowed, if the current paragraph is not empty
1031 // or caption and if it has not the keepempty flag active
1032 if (cur.lastpos() == 0 && !cpar.allowEmpty()
1033 && layout->labeltype != LABEL_SENSITIVE)
1036 // a layout change may affect also the following paragraph
1037 recUndo(cur.pit(), undoSpan(cur.pit()) - 1);
1039 // Always break behind a space
1040 // It is better to erase the space (Dekel)
1041 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
1042 cpar.erase(cur.pos());
1044 // How should the layout for the new paragraph be?
1045 int preserve_layout = 0;
1047 preserve_layout = 2;
1049 preserve_layout = layout->isEnvironment();
1051 // We need to remember this before we break the paragraph, because
1052 // that invalidates the layout variable
1053 bool sensitive = layout->labeltype == LABEL_SENSITIVE;
1055 // we need to set this before we insert the paragraph.
1056 bool const isempty = cpar.allowEmpty() && cpar.empty();
1058 ::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
1059 cur.pos(), preserve_layout);
1061 // After this, neither paragraph contains any rows!
1064 pit_type next_par = cpit + 1;
1066 // well this is the caption hack since one caption is really enough
1069 // set to standard-layout
1070 pars_[cpit].applyLayout(tclass.defaultLayout());
1072 // set to standard-layout
1073 pars_[next_par].applyLayout(tclass.defaultLayout());
1076 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
1077 pars_[next_par].erase(0);
1079 updateCounters(cur.buffer());
1081 // This check is necessary. Otherwise the new empty paragraph will
1082 // be deleted automatically. And it is more friendly for the user!
1083 if (cur.pos() != 0 || isempty)
1084 setCursor(cur, cur.pit() + 1, 0);
1086 setCursor(cur, cur.pit(), 0);
1090 // insert a character, moves all the following breaks in the
1091 // same Paragraph one to the right and make a rebreak
1092 void LyXText::insertChar(LCursor & cur, char c)
1094 BOOST_ASSERT(this == cur.text());
1095 BOOST_ASSERT(c != Paragraph::META_INSET);
1097 recordUndo(cur, Undo::INSERT);
1099 Paragraph & par = cur.paragraph();
1100 // try to remove this
1101 pit_type const pit = cur.pit();
1103 bool const freeSpacing = par.layout()->free_spacing ||
1104 par.isFreeSpacing();
1106 if (lyxrc.auto_number) {
1107 static string const number_operators = "+-/*";
1108 static string const number_unary_operators = "+-";
1109 static string const number_seperators = ".,:";
1111 if (current_font.number() == LyXFont::ON) {
1112 if (!IsDigit(c) && !contains(number_operators, c) &&
1113 !(contains(number_seperators, c) &&
1115 cur.pos() != cur.lastpos() &&
1116 getFont(par, cur.pos()).number() == LyXFont::ON &&
1117 getFont(par, cur.pos() - 1).number() == LyXFont::ON)
1119 number(cur); // Set current_font.number to OFF
1120 } else if (IsDigit(c) &&
1121 real_current_font.isVisibleRightToLeft()) {
1122 number(cur); // Set current_font.number to ON
1124 if (cur.pos() != 0) {
1125 char const c = par.getChar(cur.pos() - 1);
1126 if (contains(number_unary_operators, c) &&
1128 || par.isSeparator(cur.pos() - 2)
1129 || par.isNewline(cur.pos() - 2))
1131 setCharFont(pit, cur.pos() - 1, current_font);
1132 } else if (contains(number_seperators, c)
1134 && getFont(par, cur.pos() - 2).number() == LyXFont::ON) {
1135 setCharFont(pit, cur.pos() - 1, current_font);
1141 // First check, if there will be two blanks together or a blank at
1142 // the beginning of a paragraph.
1143 // I decided to handle blanks like normal characters, the main
1144 // difference are the special checks when calculating the row.fill
1145 // (blank does not count at the end of a row) and the check here
1147 // The bug is triggered when we type in a description environment:
1148 // The current_font is not changed when we go from label to main text
1149 // and it should (along with realtmpfont) when we type the space.
1150 // CHECK There is a bug here! (Asger)
1152 // store the current font. This is because of the use of cursor
1153 // movements. The moving cursor would refresh the current font
1154 LyXFont realtmpfont = real_current_font;
1155 LyXFont rawtmpfont = current_font;
1157 // When the free-spacing option is set for the current layout,
1158 // disable the double-space checking
1159 if (!freeSpacing && IsLineSeparatorChar(c)) {
1160 if (cur.pos() == 0) {
1161 static bool sent_space_message = false;
1162 if (!sent_space_message) {
1163 cur.message(_("You cannot insert a space at the "
1164 "beginning of a paragraph. Please read the Tutorial."));
1165 sent_space_message = true;
1169 BOOST_ASSERT(cur.pos() > 0);
1170 if (par.isLineSeparator(cur.pos() - 1)
1171 || par.isNewline(cur.pos() - 1)) {
1172 static bool sent_space_message = false;
1173 if (!sent_space_message) {
1174 cur.message(_("You cannot type two spaces this way. "
1175 "Please read the Tutorial."));
1176 sent_space_message = true;
1182 par.insertChar(cur.pos(), c, rawtmpfont);
1184 current_font = rawtmpfont;
1185 real_current_font = realtmpfont;
1186 setCursor(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1191 void LyXText::charInserted()
1193 // Here we call finishUndo for every 20 characters inserted.
1194 // This is from my experience how emacs does it. (Lgb)
1195 static unsigned int counter;
1206 LyXText::computeRowMetrics(pit_type const pit, Row const & row) const
1209 Paragraph const & par = pars_[pit];
1211 double w = dim_.wid - row.width();
1213 bool const is_rtl = isRTL(par);
1215 result.x = rightMargin(par);
1217 result.x = leftMargin(pit, row.pos());
1219 // is there a manual margin with a manual label
1220 LyXLayout_ptr const & layout = par.layout();
1222 if (layout->margintype == MARGIN_MANUAL
1223 && layout->labeltype == LABEL_MANUAL) {
1224 /// We might have real hfills in the label part
1225 int nlh = numberOfLabelHfills(par, row);
1227 // A manual label par (e.g. List) has an auto-hfill
1228 // between the label text and the body of the
1230 // But we don't want to do this auto hfill if the par
1235 if (nlh && !par.getLabelWidthString().empty())
1236 result.label_hfill = labelFill(par, row) / double(nlh);
1239 // are there any hfills in the row?
1240 int const nh = numberOfHfills(par, row);
1244 result.hfill = w / nh;
1245 // we don't have to look at the alignment if it is ALIGN_LEFT and
1246 // if the row is already larger then the permitted width as then
1247 // we force the LEFT_ALIGN'edness!
1248 } else if (int(row.width()) < maxwidth_) {
1249 // is it block, flushleft or flushright?
1250 // set x how you need it
1252 if (par.params().align() == LYX_ALIGN_LAYOUT)
1253 align = layout->align;
1255 align = par.params().align();
1257 // Display-style insets should always be on a centred row
1258 // The test on par.size() is to catch zero-size pars, which
1259 // would trigger the assert in Paragraph::getInset().
1260 //inset = par.size() ? par.getInset(row.pos()) : 0;
1262 && par.isInset(row.pos())
1263 && par.getInset(row.pos())->display())
1265 align = LYX_ALIGN_CENTER;
1269 case LYX_ALIGN_BLOCK: {
1270 int const ns = numberOfSeparators(par, row);
1271 bool disp_inset = false;
1272 if (row.endpos() < par.size()) {
1273 InsetBase const * in = par.getInset(row.endpos());
1275 disp_inset = in->display();
1277 // If we have separators, this is not the last row of a
1278 // par, does not end in newline, and is not row above a
1279 // display inset... then stretch it
1281 && row.endpos() < par.size()
1282 && !par.isNewline(row.endpos() - 1)
1285 result.separator = w / ns;
1286 } else if (is_rtl) {
1291 case LYX_ALIGN_RIGHT:
1294 case LYX_ALIGN_CENTER:
1300 bidi.computeTables(par, *bv()->buffer(), row);
1302 pos_type body_pos = par.beginOfBody();
1303 pos_type end = row.endpos();
1306 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1308 result.x += font_metrics::width(layout->labelsep, getLabelFont(par));
1309 if (body_pos <= end)
1310 result.x += result.label_hfill;
1318 // the cursor set functions have a special mechanism. When they
1319 // realize, that you left an empty paragraph, they will delete it.
1321 bool LyXText::cursorRightOneWord(LCursor & cur)
1323 BOOST_ASSERT(this == cur.text());
1327 if (old.pos() == old.lastpos() && old.pit() != old.lastpit()) {
1331 // Skip through initial nonword stuff.
1332 // Treat floats and insets as words.
1333 while (old.pos() != old.lastpos() && !old.paragraph().isLetter(old.pos()))
1335 // Advance through word.
1336 while (old.pos() != old.lastpos() && old.paragraph().isLetter(old.pos()))
1339 return setCursor(cur, old.pit(), old.pos());
1343 bool LyXText::cursorLeftOneWord(LCursor & cur)
1345 BOOST_ASSERT(this == cur.text());
1349 if (old.pos() == 0 && old.pit() != 0) {
1351 old.pos() = old.lastpos();
1353 // Skip through initial nonword stuff.
1354 // Treat floats and insets as words.
1355 while (old.pos() != 0 && !old.paragraph().isLetter(old.pos() - 1))
1357 // Advance through word.
1358 while (old.pos() != 0 && old.paragraph().isLetter(old.pos() - 1))
1361 return setCursor(cur, old.pit(), old.pos());
1365 void LyXText::selectWord(LCursor & cur, word_location loc)
1367 BOOST_ASSERT(this == cur.text());
1368 CursorSlice from = cur.top();
1369 CursorSlice to = cur.top();
1370 getWord(from, to, loc);
1371 if (cur.top() != from)
1372 setCursor(cur, from.pit(), from.pos());
1376 setCursor(cur, to.pit(), to.pos());
1381 // Select the word currently under the cursor when no
1382 // selection is currently set
1383 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1385 BOOST_ASSERT(this == cur.text());
1386 if (cur.selection())
1388 selectWord(cur, loc);
1389 return cur.selection();
1393 void LyXText::acceptChange(LCursor & cur)
1395 BOOST_ASSERT(this == cur.text());
1396 if (!cur.selection() && cur.lastpos() != 0)
1399 CursorSlice const & startc = cur.selBegin();
1400 CursorSlice const & endc = cur.selEnd();
1401 if (startc.pit() == endc.pit()) {
1402 recordUndoSelection(cur, Undo::INSERT);
1403 pars_[startc.pit()].acceptChange(startc.pos(), endc.pos());
1405 cur.clearSelection();
1406 setCursorIntern(cur, startc.pit(), 0);
1408 #ifdef WITH_WARNINGS
1409 #warning handle multi par selection
1414 void LyXText::rejectChange(LCursor & cur)
1416 BOOST_ASSERT(this == cur.text());
1417 if (!cur.selection() && cur.lastpos() != 0)
1420 CursorSlice const & startc = cur.selBegin();
1421 CursorSlice const & endc = cur.selEnd();
1422 if (startc.pit() == endc.pit()) {
1423 recordUndoSelection(cur, Undo::INSERT);
1424 pars_[startc.pit()].rejectChange(startc.pos(), endc.pos());
1426 cur.clearSelection();
1427 setCursorIntern(cur, startc.pit(), 0);
1429 #ifdef WITH_WARNINGS
1430 #warning handle multi par selection
1435 // Delete from cursor up to the end of the current or next word.
1436 void LyXText::deleteWordForward(LCursor & cur)
1438 BOOST_ASSERT(this == cur.text());
1439 if (cur.lastpos() == 0)
1443 cur.selection() = true;
1444 cursorRightOneWord(cur);
1446 cutSelection(cur, true, false);
1451 // Delete from cursor to start of current or prior word.
1452 void LyXText::deleteWordBackward(LCursor & cur)
1454 BOOST_ASSERT(this == cur.text());
1455 if (cur.lastpos() == 0)
1459 cur.selection() = true;
1460 cursorLeftOneWord(cur);
1462 cutSelection(cur, true, false);
1467 // Kill to end of line.
1468 void LyXText::deleteLineForward(LCursor & cur)
1470 BOOST_ASSERT(this == cur.text());
1471 if (cur.lastpos() == 0) {
1472 // Paragraph is empty, so we just go to the right
1476 cur.selection() = true; // to avoid deletion
1479 // What is this test for ??? (JMarc)
1480 if (!cur.selection())
1481 deleteWordForward(cur);
1483 cutSelection(cur, true, false);
1488 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1490 BOOST_ASSERT(this == cur.text());
1494 if (cur.selection()) {
1495 from = cur.selBegin();
1499 getWord(from, to, lyx::PARTIAL_WORD);
1500 setCursor(cur, to.pit(), to.pos() + 1);
1503 recordUndoSelection(cur);
1505 pos_type pos = from.pos();
1506 int par = from.pit();
1508 while (par != int(pars_.size()) && (pos != to.pos() || par != to.pit())) {
1510 if (pos == pars_[pit].size()) {
1515 unsigned char c = pars_[pit].getChar(pos);
1516 if (c != Paragraph::META_INSET) {
1518 case text_lowercase:
1521 case text_capitalization:
1523 action = text_lowercase;
1525 case text_uppercase:
1530 #ifdef WITH_WARNINGS
1533 pars_[pit].setChar(pos, c);
1539 void LyXText::Delete(LCursor & cur)
1541 BOOST_ASSERT(this == cur.text());
1542 if (cur.pos() != cur.lastpos()) {
1543 recordUndo(cur, Undo::DELETE, cur.pit());
1544 setCursorIntern(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1547 // should we do anything in an else branch?
1551 void LyXText::backspace(LCursor & cur)
1553 BOOST_ASSERT(this == cur.text());
1554 if (cur.pos() == 0) {
1555 // The cursor is at the beginning of a paragraph, so
1556 // the the backspace will collapse two paragraphs into
1559 // but it's not allowed unless it's new
1560 Paragraph & par = cur.paragraph();
1561 if (par.isChangeEdited(0, par.size()))
1564 // we may paste some paragraphs
1566 // is it an empty paragraph?
1567 pos_type lastpos = cur.lastpos();
1568 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1569 // This is an empty paragraph and we delete it just
1570 // by moving the cursor one step
1571 // left and let the DeleteEmptyParagraphMechanism
1572 // handle the actual deletion of the paragraph.
1574 if (cur.pit() != 0) {
1581 recordUndo(cur, Undo::DELETE, cur.pit() - 1);
1583 pit_type tmppit = cur.pit();
1584 // We used to do cursorLeftIntern() here, but it is
1585 // not a good idea since it triggers the auto-delete
1586 // mechanism. So we do a cursorLeftIntern()-lite,
1587 // without the dreaded mechanism. (JMarc)
1588 if (cur.pit() != 0) {
1589 // steps into the above paragraph.
1590 setCursorIntern(cur, cur.pit() - 1,
1591 pars_[cur.pit() - 1].size(),
1595 // Pasting is not allowed, if the paragraphs have different
1596 // layout. I think it is a real bug of all other
1597 // word processors to allow it. It confuses the user.
1598 // Correction: Pasting is always allowed with standard-layout
1599 Buffer & buf = cur.buffer();
1600 BufferParams const & bufparams = buf.params();
1601 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1602 pit_type const cpit = cur.pit();
1605 && (pars_[cpit].layout() == pars_[tmppit].layout()
1606 || pars_[tmppit].layout() == tclass.defaultLayout())
1607 && pars_[cpit].getAlign() == pars_[tmppit].getAlign()) {
1608 mergeParagraph(bufparams, pars_, cpit);
1610 if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
1613 // the counters may have changed
1614 updateCounters(cur.buffer());
1615 setCursor(cur, cur.pit(), cur.pos(), false);
1618 // this is the code for a normal backspace, not pasting
1620 recordUndo(cur, Undo::DELETE);
1621 // We used to do cursorLeftIntern() here, but it is
1622 // not a good idea since it triggers the auto-delete
1623 // mechanism. So we do a cursorLeftIntern()-lite,
1624 // without the dreaded mechanism. (JMarc)
1625 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1626 false, cur.boundary());
1627 cur.paragraph().erase(cur.pos());
1630 if (cur.pos() == cur.lastpos())
1631 setCurrentFont(cur);
1633 setCursor(cur, cur.pit(), cur.pos(), false, cur.boundary());
1637 Paragraph & LyXText::getPar(pit_type par) const
1639 //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
1640 BOOST_ASSERT(par >= 0);
1641 BOOST_ASSERT(par < int(paragraphs().size()));
1642 return paragraphs()[par];
1646 Row const & LyXText::firstRow() const
1648 return *paragraphs().front().rows().begin();
1652 void LyXText::redoParagraph(pit_type const pit)
1654 // remove rows of paragraph, keep track of height changes
1655 Paragraph & par = pars_[pit];
1657 // Add bibitem insets if necessary
1658 if (par.layout()->labeltype == LABEL_BIBLIO) {
1659 bool hasbibitem(false);
1660 if (!par.insetlist.empty()
1661 // Insist on it being in pos 0
1662 && par.getChar(0) == Paragraph::META_INSET) {
1663 InsetBase * inset = par.insetlist.begin()->inset;
1664 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
1668 InsetBibitem * inset(new
1669 InsetBibitem(InsetCommandParams("bibitem")));
1670 par.insertInset(0, static_cast<InsetBase *>(inset));
1675 InsetList::iterator ii = par.insetlist.begin();
1676 InsetList::iterator iend = par.insetlist.end();
1677 for (; ii != iend; ++ii) {
1679 int const w = maxwidth_ - leftMargin(pit) - rightMargin(par);
1680 MetricsInfo mi(bv(), getFont(par, ii->pos), w);
1681 ii->inset->metrics(mi, dim);
1684 // rebreak the paragraph
1688 par.setBeginOfBody();
1692 rowBreakPoint(pit, row);
1693 setRowWidth(pit, row);
1694 setHeightOfRow(pit, row);
1695 par.rows().push_back(row);
1696 dim.wid = std::max(dim.wid, row.width());
1697 dim.des += row.height();
1699 } while (z < par.size());
1701 dim.asc += par.rows()[0].ascent();
1702 dim.des -= par.rows()[0].ascent();
1704 //lyxerr << "redoParagraph: " << par.rows().size() << " rows\n";
1708 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1710 //BOOST_ASSERT(mi.base.textwidth);
1711 if (mi.base.textwidth)
1712 maxwidth_ = mi.base.textwidth;
1713 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1714 // << " maxWidth: " << maxwidth_ << "\nfont: " << mi.base.font << endl;
1715 // save the caller's font locally:
1716 font_ = mi.base.font;
1720 for (pit_type pit = 0, n = paragraphs().size(); pit != n; ++pit) {
1722 Paragraph & par = paragraphs()[pit];
1724 if (w < par.width())
1729 dim.asc = pars_[0].ascent();
1730 dim.des = h - dim.asc;
1736 // only used for inset right now. should also be used for main text
1737 void LyXText::draw(PainterInfo & pi, int x, int y) const
1739 paintTextInset(*this, pi, x, y);
1744 // only used for inset right now. should also be used for main text
1745 void LyXText::drawSelection(PainterInfo & pi, int x , int) const
1747 LCursor & cur = pi.base.bv->cursor();
1748 if (!cur.selection())
1750 if (!ptr_cmp(cur.text(), this))
1753 lyxerr << "draw selection at " << x << endl;
1755 // is there a better way of getting these two iterators?
1756 DocIterator beg = cur;
1757 DocIterator end = cur;
1759 beg.top() = cur.selBegin();
1760 end.top() = cur.selEnd();
1762 // the selection doesn't touch the visible screen
1763 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
1764 || bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_ABOVE)
1767 Paragraph const & par1 = pars_[beg.pit()];
1768 Paragraph const & par2 = pars_[end.pit()];
1770 Row const & row1 = par1.getRow(beg.pos());
1771 Row const & row2 = par2.getRow(end.pos());
1774 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_ABOVE) {
1779 y1 = bv_funcs::getPos(beg).y_ - row1.ascent();
1780 int const startx = cursorX(beg.top());
1781 x1 = isRTL(par1) ? startx : 0;
1782 x2 = isRTL(par1) ? 0 + dim_.wid : startx;
1786 if (bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_BELOW) {
1787 y2 = pi.base.bv->workHeight();
1791 y2 = bv_funcs::getPos(end).y_ + row2.descent();
1792 int const endx = cursorX(end.top());
1793 X1 = isRTL(par2) ? 0 : endx;
1794 X2 = isRTL(par2) ? endx : 0 + dim_.wid;
1797 lyxerr << " y1: " << y1 << " y2: " << y2
1798 << " xo: " << xo_ << " wid: " << dim_.wid
1801 // paint big rectangle in one go
1802 pi.pain.fillRectangle(x, y1, dim_.wid, y2 - y1, LColor::selection);
1804 // reset background at begin of first selected line
1805 pi.pain.fillRectangle(x + x1, y1, x2 - x1, row1.height(),
1806 LColor::background);
1808 // reset background at end of last selected line
1809 pi.pain.fillRectangle(x + X1, y2 - row2.height(),
1810 X2 - X1, row2.height(), LColor::background);
1815 void LyXText::drawSelection(PainterInfo & pi, int x, int) const
1817 LCursor & cur = pi.base.bv->cursor();
1818 if (!cur.selection())
1820 if (!ptr_cmp(cur.text(), this))
1823 lyxerr << "draw selection at " << x << endl;
1825 // is there a better way of getting these two iterators?
1826 DocIterator beg = cur;
1827 DocIterator end = cur;
1829 beg.top() = cur.selBegin();
1830 end.top() = cur.selEnd();
1832 // the selection doesn't touch the visible screen
1833 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
1834 || bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_ABOVE)
1837 Paragraph const & par1 = pars_[beg.pit()];
1838 Paragraph const & par2 = pars_[end.pit()];
1840 bool const above = (bv_funcs::status(pi.base.bv, beg)
1841 == bv_funcs::CUR_ABOVE);
1842 bool const below = (bv_funcs::status(pi.base.bv, end)
1843 == bv_funcs::CUR_BELOW);
1851 Row const & row1 = par1.getRow(beg.pos());
1852 y1 = bv_funcs::getPos(beg).y_ - row1.ascent();
1853 y2 = y1 + row1.height();
1854 int const startx = cursorX(beg.top());
1855 x1 = !isRTL(par1) ? startx : 0;
1856 x2 = !isRTL(par1) ? 0 + dim_.wid : startx;
1861 Y1 = pi.base.bv->workHeight();
1862 Y2 = pi.base.bv->workHeight();
1866 Row const & row2 = par2.getRow(end.pos());
1867 Y1 = bv_funcs::getPos(end).y_ - row2.ascent();
1868 Y2 = Y1 + row2.height();
1869 int const endx = cursorX(end.top());
1870 X1 = !isRTL(par2) ? 0 : endx;
1871 X2 = !isRTL(par2) ? endx : 0 + dim_.wid;
1874 if (!above && !below && &par1.getRow(beg.pos())
1875 == &par2.getRow(end.pos()))
1877 // paint only one rectangle
1878 pi.pain.fillRectangle(x + x1, y1, X2 - x1, y2 - y1,
1883 // paint upper rectangle
1884 pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
1886 // paint bottom rectangle
1887 pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
1889 // paint center rectangle
1890 pi.pain.fillRectangle(x, y2, dim_.wid,
1891 Y1 - y2, LColor::selection);
1894 bool LyXText::isLastRow(pit_type pit, Row const & row) const
1896 return row.endpos() >= pars_[pit].size()
1897 && pit + 1 == pit_type(paragraphs().size());
1901 bool LyXText::isFirstRow(pit_type pit, Row const & row) const
1903 return row.pos() == 0 && pit == 0;
1907 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1908 word_location const loc)
1910 Paragraph const & from_par = pars_[from.pit()];
1912 case lyx::WHOLE_WORD_STRICT:
1913 if (from.pos() == 0 || from.pos() == from_par.size()
1914 || !from_par.isLetter(from.pos())
1915 || !from_par.isLetter(from.pos() - 1)) {
1919 // no break here, we go to the next
1921 case lyx::WHOLE_WORD:
1922 // If we are already at the beginning of a word, do nothing
1923 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
1925 // no break here, we go to the next
1927 case lyx::PREVIOUS_WORD:
1928 // always move the cursor to the beginning of previous word
1929 while (from.pos() && from_par.isLetter(from.pos() - 1))
1932 case lyx::NEXT_WORD:
1933 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1936 case lyx::PARTIAL_WORD:
1937 // no need to move the 'from' cursor
1941 Paragraph & to_par = pars_[to.pit()];
1942 while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
1947 void LyXText::write(Buffer const & buf, std::ostream & os) const
1949 ParagraphList::const_iterator pit = paragraphs().begin();
1950 ParagraphList::const_iterator end = paragraphs().end();
1951 Paragraph::depth_type dth = 0;
1952 for (; pit != end; ++pit)
1953 pit->write(buf, os, buf.params(), dth);
1957 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1959 static Change current_change;
1961 Paragraph::depth_type depth = 0;
1963 while (lex.isOK()) {
1965 string const token = lex.getString();
1970 if (token == "\\end_inset") {
1974 if (token == "\\end_body") {
1978 if (token == "\\begin_body") {
1982 if (token == "\\end_document") {
1986 if (token == "\\begin_layout") {
1987 lex.pushToken(token);
1990 par.params().depth(depth);
1991 if (buf.params().tracking_changes)
1993 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1994 pars_.push_back(par);
1996 // FIXME: goddamn InsetTabular makes us pass a Buffer
1998 ::readParagraph(buf, pars_.back(), lex);
2000 } else if (token == "\\begin_deeper") {
2002 } else if (token == "\\end_deeper") {
2004 lex.printError("\\end_deeper: " "depth is already null");
2009 lyxerr << "Handling unknown body token: `"
2010 << token << '\'' << endl;
2017 int LyXText::ascent() const
2023 int LyXText::descent() const
2029 int LyXText::cursorX(CursorSlice const & cur) const
2031 pit_type const pit = cur.pit();
2032 Paragraph const & par = pars_[pit];
2033 if (par.rows().empty())
2036 Row const & row = par.getRow(cur.pos());
2038 pos_type pos = cur.pos();
2039 pos_type cursor_vpos = 0;
2041 RowMetrics const m = computeRowMetrics(pit, row);
2044 pos_type const row_pos = row.pos();
2045 pos_type const end = row.endpos();
2048 cursor_vpos = row_pos;
2049 else if (pos >= end)
2050 cursor_vpos = isRTL(par) ? row_pos : end;
2051 else if (pos > row_pos && pos >= end)
2052 // Place cursor after char at (logical) position pos - 1
2053 cursor_vpos = (bidi.level(pos - 1) % 2 == 0)
2054 ? bidi.log2vis(pos - 1) + 1 : bidi.log2vis(pos - 1);
2056 // Place cursor before char at (logical) position pos
2057 cursor_vpos = (bidi.level(pos) % 2 == 0)
2058 ? bidi.log2vis(pos) : bidi.log2vis(pos) + 1;
2060 pos_type body_pos = par.beginOfBody();
2062 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
2065 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
2066 pos_type pos = bidi.vis2log(vpos);
2067 if (body_pos > 0 && pos == body_pos - 1) {
2069 + font_metrics::width(par.layout()->labelsep,
2071 if (par.isLineSeparator(body_pos - 1))
2072 x -= singleWidth(par, body_pos - 1);
2075 if (hfillExpansion(par, row, pos)) {
2076 x += singleWidth(par, pos);
2077 if (pos >= body_pos)
2081 } else if (par.isSeparator(pos)) {
2082 x += singleWidth(par, pos);
2083 if (pos >= body_pos)
2086 x += singleWidth(par, pos);
2092 int LyXText::cursorY(CursorSlice const & cur) const
2094 Paragraph const & par = getPar(cur.pit());
2096 h -= pars_[0].rows()[0].ascent();
2097 for (pit_type pit = 0; pit < cur.pit(); ++pit)
2098 h += pars_[pit].height();
2099 for (size_t rit = 0, rend = par.pos2row(cur.pos()); rit != rend; ++rit)
2100 h += par.rows()[rit].height();
2101 h += par.rows()[par.pos2row(cur.pos())].ascent();
2106 // Returns the current font and depth as a message.
2107 string LyXText::currentState(LCursor & cur)
2109 BOOST_ASSERT(this == cur.text());
2110 Buffer & buf = cur.buffer();
2111 Paragraph const & par = cur.paragraph();
2112 std::ostringstream os;
2114 bool const show_change = buf.params().tracking_changes
2115 && cur.pos() != cur.lastpos()
2116 && par.lookupChange(cur.pos()) != Change::UNCHANGED;
2119 Change change = par.lookupChangeFull(cur.pos());
2120 Author const & a = buf.params().authors().get(change.author);
2121 os << _("Change: ") << a.name();
2122 if (!a.email().empty())
2123 os << " (" << a.email() << ")";
2124 if (change.changetime)
2125 os << _(" at ") << ctime(&change.changetime);
2129 // I think we should only show changes from the default
2131 LyXFont font = real_current_font;
2132 font.reduce(buf.params().getLyXTextClass().defaultfont());
2134 // avoid _(...) re-entrance problem
2135 string const s = font.stateText(&buf.params());
2136 os << bformat(_("Font: %1$s"), s);
2138 // os << bformat(_("Font: %1$s"), font.stateText(&buf.params));
2140 // The paragraph depth
2141 int depth = cur.paragraph().getDepth();
2143 os << bformat(_(", Depth: %1$d"), depth);
2145 // The paragraph spacing, but only if different from
2147 Spacing const & spacing = par.params().spacing();
2148 if (!spacing.isDefault()) {
2149 os << _(", Spacing: ");
2150 switch (spacing.getSpace()) {
2151 case Spacing::Single:
2154 case Spacing::Onehalf:
2157 case Spacing::Double:
2160 case Spacing::Other:
2161 os << _("Other (") << spacing.getValueAsString() << ')';
2163 case Spacing::Default:
2164 // should never happen, do nothing
2169 #ifdef DEVEL_VERSION
2170 os << _(", Inset: ") << &cur.inset();
2171 os << _(", Paragraph: ") << cur.pit();
2172 os << _(", Id: ") << par.id();
2173 os << _(", Position: ") << cur.pos();
2174 // Row & row = cur.textRow();
2175 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2181 string LyXText::getPossibleLabel(LCursor & cur) const
2183 pit_type pit = cur.pit();
2185 LyXLayout_ptr layout = pars_[pit].layout();
2187 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2188 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
2189 if (layout2->latextype != LATEX_PARAGRAPH) {
2195 string text = layout->latexname().substr(0, 3);
2196 if (layout->latexname() == "theorem")
2197 text = "thm"; // Create a correct prefix for prettyref
2200 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2203 string par_text = pars_[pit].asString(cur.buffer(), false);
2204 for (int i = 0; i < lyxrc.label_init_length; ++i) {
2205 if (par_text.empty())
2208 par_text = split(par_text, head, ' ');
2209 // Is it legal to use spaces in labels ?
2219 //pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2223 // Paragraph const & par = pars_[pit];
2224 // Row const & r = par.rows()[row];
2225 // int pos = r.pos();
2226 // for (; currx < x && pos < r.endpos(); ++pos) {
2228 // currx += singleWidth(par, pos);
2230 // if (abs(lastx - x) < abs(currx - x) && pos != r.pos())
2236 pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2238 BOOST_ASSERT(row < pars_[pit].rows().size());
2240 Row const & r = pars_[pit].rows()[row];
2241 return r.pos() + getColumnNearX(pit, r, x, bound);
2245 //int LyXText::pos2x(pit_type pit, pos_type pos) const
2247 // Paragraph const & par = pars_[pit];
2248 // Row const & r = par.rows()[row];
2254 // x,y are screen coordinates
2255 // sets cursor only within this LyXText
2256 void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
2258 pit_type pit = getPitNearY(y);
2259 int yy = theCoords.get(this, pit).y_ - pars_[pit].ascent();
2260 lyxerr << "setCursorFromCoordinates: x: " << x << " y: " << y
2261 << " pit: " << pit << " yy: " << yy << endl;
2263 Paragraph const & par = pars_[pit];
2265 BOOST_ASSERT(par.rows().size());
2266 for (; r < int(par.rows().size()) - 1; ++r) {
2267 Row const & row = par.rows()[r];
2268 if (int(yy + row.height()) > y)
2273 Row const & row = par.rows()[r];
2275 lyxerr << "setCursorFromCoordinates: row " << r
2276 << " from pos: " << row.pos() << endl;
2280 pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
2281 setCursor(cur, pit, pos, true, bound);