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 "bufferparams.h"
24 #include "BufferView.h"
26 #include "CutAndPaste.h"
28 #include "dispatchresult.h"
30 #include "errorlist.h"
31 #include "funcrequest.h"
33 #include "FontIterator.h"
37 #include "lyxlength.h"
41 #include "lyxrow_funcs.h"
42 #include "metricsinfo.h"
43 #include "paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "rowpainter.h"
49 #include "WordLangTuple.h"
51 #include "frontends/font_metrics.h"
52 #include "frontends/LyXView.h"
54 #include "insets/insettext.h"
55 #include "insets/insetbibitem.h"
56 #include "insets/insethfill.h"
57 #include "insets/insetlatexaccent.h"
58 #include "insets/insetline.h"
59 #include "insets/insetnewline.h"
60 #include "insets/insetpagebreak.h"
61 #include "insets/insetoptarg.h"
62 #include "insets/insetspace.h"
63 #include "insets/insetspecialchar.h"
64 #include "insets/insettabular.h"
66 #include "support/lstrings.h"
67 #include "support/textutils.h"
68 #include "support/tostr.h"
74 using lyx::word_location;
76 using lyx::support::bformat;
77 using lyx::support::contains;
78 using lyx::support::lowercase;
79 using lyx::support::split;
80 using lyx::support::uppercase;
82 using lyx::cap::cutSelection;
93 /// some space for drawing the 'nested' markers (in pixel)
94 extern int const NEST_MARGIN = 20;
95 /// margin for changebar
96 extern int const CHANGEBAR_MARGIN = 10;
98 extern int const RIGHT_MARGIN = 10;
103 int numberOfSeparators(Paragraph const & par, Row const & row)
105 pos_type const first = max(row.pos(), par.beginOfBody());
106 pos_type const last = row.endpos() - 1;
108 for (pos_type p = first; p < last; ++p) {
109 if (par.isSeparator(p))
116 unsigned int maxParagraphWidth(ParagraphList const & plist)
118 unsigned int width = 0;
119 ParagraphList::const_iterator pit = plist.begin();
120 ParagraphList::const_iterator end = plist.end();
121 for (; pit != end; ++pit)
122 width = std::max(width, pit->width);
127 int numberOfLabelHfills(Paragraph const & par, Row const & row)
129 pos_type last = row.endpos() - 1;
130 pos_type first = row.pos();
132 // hfill *DO* count at the beginning of paragraphs!
134 while (first < last && par.isHfill(first))
138 last = min(last, par.beginOfBody());
140 for (pos_type p = first; p < last; ++p) {
148 int numberOfHfills(Paragraph const & par, Row const & row)
150 pos_type const last = row.endpos() - 1;
151 pos_type first = row.pos();
153 // hfill *DO* count at the beginning of paragraphs!
155 while (first < last && par.isHfill(first))
159 first = max(first, par.beginOfBody());
162 for (pos_type p = first; p < last; ++p) {
170 int readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
171 string const & token)
174 static Change change;
176 BufferParams const & bp = buf.params();
178 if (token[0] != '\\') {
179 string::const_iterator cit = token.begin();
180 for (; cit != token.end(); ++cit) {
181 par.insertChar(par.size(), (*cit), font, change);
183 } else if (token == "\\begin_layout") {
185 string layoutname = lex.getString();
187 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
190 LyXTextClass const & tclass = bp.getLyXTextClass();
192 if (layoutname.empty()) {
193 layoutname = tclass.defaultLayoutName();
196 bool hasLayout = tclass.hasLayout(layoutname);
199 lyxerr << "Layout '" << layoutname << "' does not"
200 << " exist in textclass '" << tclass.name()
202 lyxerr << "Trying to use default layout instead."
204 layoutname = tclass.defaultLayoutName();
207 par.layout(bp.getLyXTextClass()[layoutname]);
209 // Test whether the layout is obsolete.
210 LyXLayout_ptr const & layout = par.layout();
211 if (!layout->obsoleted_by().empty())
212 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
214 par.params().read(lex);
216 } else if (token == "\\end_layout") {
217 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
218 << "Missing \\begin_layout?.\n";
219 } else if (token == "\\end_inset") {
220 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
221 << "Missing \\begin_inset?.\n";
222 } else if (token == "\\begin_inset") {
223 InsetBase * inset = readInset(lex, buf);
225 par.insertInset(par.size(), inset, font, change);
228 string line = lex.getString();
229 buf.error(ErrorItem(_("Unknown Inset"), line,
230 par.id(), 0, par.size()));
233 } else if (token == "\\family") {
235 font.setLyXFamily(lex.getString());
236 } else if (token == "\\series") {
238 font.setLyXSeries(lex.getString());
239 } else if (token == "\\shape") {
241 font.setLyXShape(lex.getString());
242 } else if (token == "\\size") {
244 font.setLyXSize(lex.getString());
245 } else if (token == "\\lang") {
247 string const tok = lex.getString();
248 Language const * lang = languages.getLanguage(tok);
250 font.setLanguage(lang);
252 font.setLanguage(bp.language);
253 lex.printError("Unknown language `$$Token'");
255 } else if (token == "\\numeric") {
257 font.setNumber(font.setLyXMisc(lex.getString()));
258 } else if (token == "\\emph") {
260 font.setEmph(font.setLyXMisc(lex.getString()));
261 } else if (token == "\\bar") {
263 string const tok = lex.getString();
266 font.setUnderbar(LyXFont::ON);
267 else if (tok == "no")
268 font.setUnderbar(LyXFont::OFF);
269 else if (tok == "default")
270 font.setUnderbar(LyXFont::INHERIT);
272 lex.printError("Unknown bar font flag "
274 } else if (token == "\\noun") {
276 font.setNoun(font.setLyXMisc(lex.getString()));
277 } else if (token == "\\color") {
279 font.setLyXColor(lex.getString());
280 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
282 // Insets don't make sense in a free-spacing context! ---Kayvan
283 if (par.isFreeSpacing()) {
284 if (token == "\\InsetSpace")
285 par.insertChar(par.size(), ' ', font, change);
286 else if (lex.isOK()) {
288 string const next_token = lex.getString();
289 if (next_token == "\\-")
290 par.insertChar(par.size(), '-', font, change);
292 lex.printError("Token `$$Token' "
294 "paragraph layout!");
298 auto_ptr<InsetBase> inset;
299 if (token == "\\SpecialChar" )
300 inset.reset(new InsetSpecialChar);
302 inset.reset(new InsetSpace);
303 inset->read(buf, lex);
304 par.insertInset(par.size(), inset.release(),
307 } else if (token == "\\i") {
308 auto_ptr<InsetBase> inset(new InsetLatexAccent);
309 inset->read(buf, lex);
310 par.insertInset(par.size(), inset.release(), font, change);
311 } else if (token == "\\backslash") {
312 par.insertChar(par.size(), '\\', font, change);
313 } else if (token == "\\newline") {
314 auto_ptr<InsetBase> inset(new InsetNewline);
315 inset->read(buf, lex);
316 par.insertInset(par.size(), inset.release(), font, change);
317 } else if (token == "\\LyXTable") {
318 auto_ptr<InsetBase> inset(new InsetTabular(buf));
319 inset->read(buf, lex);
320 par.insertInset(par.size(), inset.release(), font, change);
321 } else if (token == "\\bibitem") {
322 InsetCommandParams p("bibitem", "dummy");
323 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
324 inset->read(buf, lex);
325 par.insertInset(par.size(), inset.release(), font, change);
326 } else if (token == "\\hfill") {
327 par.insertInset(par.size(), new InsetHFill, font, change);
328 } else if (token == "\\lyxline") {
329 par.insertInset(par.size(), new InsetLine, font, change);
330 } else if (token == "\\newpage") {
331 par.insertInset(par.size(), new InsetPagebreak, font, change);
332 } else if (token == "\\change_unchanged") {
333 // Hack ! Needed for empty paragraphs :/
334 // FIXME: is it still ??
337 change = Change(Change::UNCHANGED);
338 } else if (token == "\\change_inserted") {
340 std::istringstream is(lex.getString());
344 change = Change(Change::INSERTED, bp.author_map[aid], ct);
345 } else if (token == "\\change_deleted") {
347 std::istringstream is(lex.getString());
351 change = Change(Change::DELETED, bp.author_map[aid], ct);
354 buf.error(ErrorItem(_("Unknown token"),
355 bformat(_("Unknown token: %1$s %2$s\n"), token, lex.getString()),
356 par.id(), 0, par.size()));
363 int readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex)
368 string token = lex.getString();
372 unknown += readParToken(buf, par, lex, token);
375 token = lex.getString();
380 if (token == "\\end_layout") {
381 //Ok, paragraph finished
385 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
386 << token << '\'' << endl;
387 if (token == "\\begin_layout" || token == "\\end_document"
388 || token == "\\end_inset" || token == "\\begin_deeper"
389 || token == "\\end_deeper") {
390 lex.pushToken(token);
391 lyxerr << "Paragraph ended in line "
392 << lex.getLineNo() << "\n"
393 << "Missing \\end_layout.\n";
405 BufferView * LyXText::bv()
407 BOOST_ASSERT(bv_owner != 0);
412 BufferView * LyXText::bv() const
414 BOOST_ASSERT(bv_owner != 0);
419 double LyXText::spacing(Paragraph const & par) const
421 if (par.params().spacing().isDefault())
422 return bv()->buffer()->params().spacing().getValue();
423 return par.params().spacing().getValue();
427 void LyXText::updateParPositions()
430 par_type end = pars_.size();
431 for (height_ = 0; pit != end; ++pit) {
432 pars_[pit].y = height_;
433 height_ += pars_[pit].height;
438 int LyXText::width() const
444 int LyXText::height() const
450 int LyXText::singleWidth(par_type par, pos_type pos) const
452 if (pos >= pars_[par].size())
455 char const c = pars_[par].getChar(pos);
456 return singleWidth(par, pos, c, getFont(par, pos));
460 int LyXText::singleWidth(par_type pit,
461 pos_type pos, char c, LyXFont const & font) const
463 if (pos >= pars_[pit].size()) {
464 lyxerr << "in singleWidth(), pos: " << pos << endl;
469 // The most common case is handled first (Asger)
470 if (IsPrintable(c)) {
471 if (!font.language()->RightToLeft()) {
472 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
473 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
474 && font.language()->lang() == "arabic") {
475 if (Encodings::IsComposeChar_arabic(c))
478 c = pars_[pit].transformChar(c, pos);
479 } else if (font.language()->lang() == "hebrew" &&
480 Encodings::IsComposeChar_hebrew(c))
483 return font_metrics::width(c, font);
486 if (c == Paragraph::META_INSET)
487 return pars_[pit].getInset(pos)->width();
489 if (IsSeparatorChar(c))
491 return font_metrics::width(c, font);
495 int LyXText::leftMargin(par_type pit) const
497 return leftMargin(pit, pars_[pit].size());
501 int LyXText::leftMargin(par_type pit, pos_type pos) const
503 LyXTextClass const & tclass =
504 bv()->buffer()->params().getLyXTextClass();
505 LyXLayout_ptr const & layout = pars_[pit].layout();
507 string parindent = layout->parindent;
509 int x = NEST_MARGIN + CHANGEBAR_MARGIN;
511 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
513 // This is the way LyX handles LaTeX-Environments.
514 // I have had this idea very late, so it seems to be a
515 // later added hack and this is true
516 if (pars_[pit].getDepth() == 0) {
517 if (pars_[pit].layout() == tclass.defaultLayout()) {
518 // find the previous same level paragraph
521 depthHook(pit, paragraphs(), pars_[pit].getDepth());
522 if (newpit == pit && pars_[newpit].layout()->nextnoindent)
527 // find the next level paragraph
528 par_type newpar = outerHook(pit, pars_);
530 // Make a corresponding row. Need to call leftMargin()
531 // to check whether it is a sufficent paragraph.
532 if (newpar != par_type(pars_.size())
533 && pars_[newpar].layout()->isEnvironment()) {
534 x = leftMargin(newpar);
537 if (newpar != par_type(paragraphs().size())
538 && pars_[pit].layout() == tclass.defaultLayout()) {
539 if (pars_[newpar].params().noindent())
542 parindent = pars_[newpar].layout()->parindent;
546 LyXFont const labelfont = getLabelFont(pit);
547 switch (layout->margintype) {
549 if (!layout->leftmargin.empty())
550 x += font_metrics::signedWidth(layout->leftmargin,
551 tclass.defaultfont());
552 if (!pars_[pit].getLabelstring().empty()) {
553 x += font_metrics::signedWidth(layout->labelindent,
555 x += font_metrics::width(pars_[pit].getLabelstring(),
557 x += font_metrics::width(layout->labelsep, labelfont);
562 x += font_metrics::signedWidth(layout->labelindent, labelfont);
563 // The width of an empty par, even with manual label, should be 0
564 if (!pars_[pit].empty() && pos >= pars_[pit].beginOfBody()) {
565 if (!pars_[pit].getLabelWidthString().empty()) {
566 x += font_metrics::width(pars_[pit].getLabelWidthString(),
568 x += font_metrics::width(layout->labelsep, labelfont);
574 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
575 / (pars_[pit].getDepth() + 4);
578 case MARGIN_FIRST_DYNAMIC:
579 if (layout->labeltype == LABEL_MANUAL) {
580 if (pos >= pars_[pit].beginOfBody()) {
581 x += font_metrics::signedWidth(layout->leftmargin,
584 x += font_metrics::signedWidth(layout->labelindent,
588 // Special case to fix problems with
590 || (layout->labeltype == LABEL_STATIC
591 && layout->latextype == LATEX_ENVIRONMENT
592 && !isFirstInSequence(pit, paragraphs()))) {
593 x += font_metrics::signedWidth(layout->leftmargin,
595 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
596 && layout->labeltype != LABEL_BIBLIO
597 && layout->labeltype !=
598 LABEL_CENTERED_TOP_ENVIRONMENT) {
599 x += font_metrics::signedWidth(layout->labelindent,
601 x += font_metrics::width(layout->labelsep, labelfont);
602 x += font_metrics::width(pars_[pit].getLabelstring(),
607 case MARGIN_RIGHT_ADDRESS_BOX: {
609 // ok, a terrible hack. The left margin depends on the widest
610 // row in this paragraph.
611 RowList::iterator rit = pars_[pit].rows.begin();
612 RowList::iterator end = pars_[pit].rows.end();
614 #warning This is wrong.
616 int minfill = maxwidth_;
617 for ( ; rit != end; ++rit)
618 if (rit->fill() < minfill)
619 minfill = rit->fill();
620 x += font_metrics::signedWidth(layout->leftmargin,
621 tclass.defaultfont());
624 // also wrong, but much shorter.
631 if (!pars_[pit].params().leftIndent().zero())
632 x += pars_[pit].params().leftIndent().inPixels(maxwidth_);
636 if (pars_[pit].params().align() == LYX_ALIGN_LAYOUT)
637 align = layout->align;
639 align = pars_[pit].params().align();
641 // set the correct parindent
643 && (layout->labeltype == LABEL_NO_LABEL
644 || layout->labeltype == LABEL_TOP_ENVIRONMENT
645 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
646 || (layout->labeltype == LABEL_STATIC
647 && layout->latextype == LATEX_ENVIRONMENT
648 && !isFirstInSequence(pit, paragraphs())))
649 && align == LYX_ALIGN_BLOCK
650 && !pars_[pit].params().noindent()
651 // in tabulars and ert paragraphs are never indented!
652 && (pars_[pit].ownerCode() != InsetOld::TABULAR_CODE
653 && pars_[pit].ownerCode() != InsetOld::ERT_CODE)
654 && (pars_[pit].layout() != tclass.defaultLayout()
655 || bv()->buffer()->params().paragraph_separation ==
656 BufferParams::PARSEP_INDENT))
658 x += font_metrics::signedWidth(parindent, tclass.defaultfont());
665 int LyXText::rightMargin(Paragraph const & par) const
667 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
671 + font_metrics::signedWidth(tclass.rightmargin(),
672 tclass.defaultfont())
673 + font_metrics::signedWidth(par.layout()->rightmargin,
674 tclass.defaultfont())
675 * 4 / (par.getDepth() + 4);
679 int LyXText::labelEnd(par_type pit) const
681 // labelEnd is only needed if the layout fills a flushleft label.
682 if (pars_[pit].layout()->margintype != MARGIN_MANUAL)
684 // return the beginning of the body
685 return leftMargin(pit);
691 // this needs special handling - only newlines count as a break point
692 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
694 pos_type const end = par.size();
697 if (par.isNewline(i))
706 void LyXText::rowBreakPoint(par_type pit, Row & row) const
708 pos_type const end = pars_[pit].size();
709 pos_type const pos = row.pos();
715 // maximum pixel width of a row
716 int width = maxwidth_ - rightMargin(pars_[pit]); // - leftMargin(pit, row);
722 LyXLayout_ptr const & layout = pars_[pit].layout();
724 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
725 row.endpos(addressBreakPoint(pos, pars_[pit]));
729 pos_type const body_pos = pars_[pit].beginOfBody();
732 // Now we iterate through until we reach the right margin
733 // or the end of the par, then choose the possible break
736 int const left = leftMargin(pit, pos);
739 // pixel width since last breakpoint
742 FontIterator fi = FontIterator(*this, pit, pos);
743 pos_type point = end;
745 for ( ; i < end; ++i, ++fi) {
746 char const c = pars_[pit].getChar(i);
749 int thiswidth = singleWidth(pit, i, c, *fi);
751 // add the auto-hfill from label end to the body
752 if (body_pos && i == body_pos) {
753 int add = font_metrics::width(layout->labelsep, getLabelFont(pit));
754 if (pars_[pit].isLineSeparator(i - 1))
755 add -= singleWidth(pit, i - 1);
757 add = std::max(add, labelEnd(pit) - x);
762 chunkwidth += thiswidth;
765 // break before a character that will fall off
766 // the right of the row
768 // if no break before, break here
769 if (point == end || chunkwidth >= width - left) {
776 // exit on last registered breakpoint:
780 if (pars_[pit].isNewline(i)) {
786 if (pars_[pit].isInset(i + 1) && pars_[pit].getInset(i + 1)->display()) {
791 if (pars_[pit].isInset(i) && pars_[pit].getInset(i)->display()) {
797 if (!pars_[pit].isInset(i) || pars_[pit].getInset(i)->isChar()) {
798 // some insets are line separators too
799 if (pars_[pit].isLineSeparator(i)) {
800 // register breakpoint:
807 // maybe found one, but the par is short enough.
808 if (i == end && x < width)
811 // manual labels cannot be broken in LaTeX. But we
812 // want to make our on-screen rendering of footnotes
814 if (body_pos && point < body_pos)
821 void LyXText::setRowWidth(par_type pit, Row & row) const
823 // get the pure distance
824 pos_type const end = row.endpos();
826 string labelsep = pars_[pit].layout()->labelsep;
827 int w = leftMargin(pit, row.pos());
829 pos_type const body_pos = pars_[pit].beginOfBody();
830 pos_type i = row.pos();
833 FontIterator fi = FontIterator(*this, pit, i);
834 for ( ; i < end; ++i, ++fi) {
835 if (body_pos > 0 && i == body_pos) {
836 w += font_metrics::width(labelsep, getLabelFont(pit));
837 if (pars_[pit].isLineSeparator(i - 1))
838 w -= singleWidth(pit, i - 1);
839 w = max(w, labelEnd(pit));
841 char const c = pars_[pit].getChar(i);
842 w += singleWidth(pit, i, c, *fi);
846 if (body_pos > 0 && body_pos >= end) {
847 w += font_metrics::width(labelsep, getLabelFont(pit));
848 if (end > 0 && pars_[pit].isLineSeparator(end - 1))
849 w -= singleWidth(pit, end - 1);
850 w = max(w, labelEnd(pit));
853 row.width(w + rightMargin(pars_[pit]));
857 // returns the minimum space a manual label needs on the screen in pixel
858 int LyXText::labelFill(par_type pit, Row const & row) const
860 pos_type last = pars_[pit].beginOfBody();
862 BOOST_ASSERT(last > 0);
864 // -1 because a label ends with a space that is in the label
867 // a separator at this end does not count
868 if (pars_[pit].isLineSeparator(last))
872 for (pos_type i = row.pos(); i <= last; ++i)
873 w += singleWidth(pit, i);
875 string const & label = pars_[pit].params().labelWidthString();
879 return max(0, font_metrics::width(label, getLabelFont(pit)) - w);
883 LColor_color LyXText::backgroundColor() const
885 return LColor_color(LColor::color(background_color_));
889 void LyXText::setHeightOfRow(par_type pit, Row & row)
891 // get the maximum ascent and the maximum descent
892 double layoutasc = 0;
893 double layoutdesc = 0;
894 double const dh = defaultRowHeight();
896 // ok, let us initialize the maxasc and maxdesc value.
897 // Only the fontsize count. The other properties
898 // are taken from the layoutfont. Nicer on the screen :)
899 LyXLayout_ptr const & layout = pars_[pit].layout();
901 // as max get the first character of this row then it can
902 // increase but not decrease the height. Just some point to
903 // start with so we don't have to do the assignment below too
905 LyXFont font = getFont(pit, row.pos());
906 LyXFont::FONT_SIZE const tmpsize = font.size();
907 font = getLayoutFont(pit);
908 LyXFont::FONT_SIZE const size = font.size();
909 font.setSize(tmpsize);
911 LyXFont labelfont = getLabelFont(pit);
913 // these are minimum values
914 double const spacing_val =
915 layout->spacing.getValue() * spacing(pars_[pit]);
916 //lyxerr << "spacing_val = " << spacing_val << endl;
917 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
918 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
920 // insets may be taller
921 InsetList::const_iterator ii = pars_[pit].insetlist.begin();
922 InsetList::const_iterator iend = pars_[pit].insetlist.end();
923 for ( ; ii != iend; ++ii) {
924 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
925 maxasc = max(maxasc, ii->inset->ascent());
926 maxdesc = max(maxdesc, ii->inset->descent());
930 // Check if any custom fonts are larger (Asger)
931 // This is not completely correct, but we can live with the small,
932 // cosmetic error for now.
934 pos_type const pos_end = row.endpos();
936 LyXFont::FONT_SIZE maxsize =
937 pars_[pit].highestFontInRange(row.pos(), pos_end, size);
938 if (maxsize > font.size()) {
939 font.setSize(maxsize);
940 maxasc = max(maxasc, font_metrics::maxAscent(font));
941 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
944 // This is nicer with box insets:
948 row.ascent_of_text(maxasc);
951 if (row.pos() == 0) {
952 BufferParams const & bufparams = bv()->buffer()->params();
953 // some parksips VERY EASY IMPLEMENTATION
954 if (bv()->buffer()->params().paragraph_separation
955 == BufferParams::PARSEP_SKIP
957 && ((layout->isParagraph() && pars_[pit].getDepth() == 0)
958 || (pars_[pit - 1].layout()->isParagraph()
959 && pars_[pit - 1].getDepth() == 0)))
961 maxasc += bufparams.getDefSkip().inPixels(*bv());
964 if (pars_[pit].params().startOfAppendix())
965 maxasc += int(3 * dh);
967 // This is special code for the chapter, since the label of this
968 // layout is printed in an extra row
969 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
970 labeladdon = int(font_metrics::maxHeight(labelfont)
971 * layout->spacing.getValue()
972 * spacing(pars_[pit]));
975 // special code for the top label
976 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
977 || layout->labeltype == LABEL_BIBLIO
978 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
979 && isFirstInSequence(pit, paragraphs())
980 && !pars_[pit].getLabelstring().empty())
983 font_metrics::maxHeight(labelfont)
984 * layout->spacing.getValue()
985 * spacing(pars_[pit])
986 + (layout->topsep + layout->labelbottomsep) * dh);
989 // Add the layout spaces, for example before and after
990 // a section, or between the items of a itemize or enumerate
993 par_type prev = depthHook(pit, pars_, pars_[pit].getDepth());
995 && pars_[prev].layout() == layout
996 && pars_[prev].getDepth() == pars_[pit].getDepth()
997 && pars_[prev].getLabelWidthString() == pars_[pit].getLabelWidthString())
999 layoutasc = layout->itemsep * dh;
1000 } else if (pit != 0 || row.pos() != 0) {
1001 if (layout->topsep > 0)
1002 layoutasc = layout->topsep * dh;
1005 prev = outerHook(pit, pars_);
1006 if (prev != par_type(pars_.size())) {
1007 maxasc += int(pars_[prev].layout()->parsep * dh);
1008 } else if (pit != 0) {
1009 if (pars_[pit - 1].getDepth() != 0 ||
1010 pars_[pit - 1].layout() == layout) {
1011 maxasc += int(layout->parsep * dh);
1016 // is it a bottom line?
1017 if (row.endpos() >= pars_[pit].size()) {
1018 // add the layout spaces, for example before and after
1019 // a section, or between the items of a itemize or enumerate
1021 par_type nextpit = pit + 1;
1022 if (nextpit != par_type(pars_.size())) {
1023 par_type cpit = pit;
1027 if (pars_[cpit].getDepth() > pars_[nextpit].getDepth()) {
1028 usual = pars_[cpit].layout()->bottomsep * dh;
1029 cpit = depthHook(cpit, paragraphs(), pars_[nextpit].getDepth());
1030 if (pars_[cpit].layout() != pars_[nextpit].layout()
1031 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1033 unusual = pars_[cpit].layout()->bottomsep * dh;
1035 layoutdesc = max(unusual, usual);
1036 } else if (pars_[cpit].getDepth() == pars_[nextpit].getDepth()) {
1037 if (pars_[cpit].layout() != pars_[nextpit].layout()
1038 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1039 layoutdesc = int(pars_[cpit].layout()->bottomsep * dh);
1044 // incalculate the layout spaces
1045 maxasc += int(layoutasc * 2 / (2 + pars_[pit].getDepth()));
1046 maxdesc += int(layoutdesc * 2 / (2 + pars_[pit].getDepth()));
1048 row.height(maxasc + maxdesc + labeladdon);
1049 row.baseline(maxasc + labeladdon);
1050 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
1054 void LyXText::breakParagraph(LCursor & cur, char keep_layout)
1056 BOOST_ASSERT(this == cur.text());
1057 // allow only if at start or end, or all previous is new text
1058 Paragraph & cpar = cur.paragraph();
1059 par_type cpit = cur.par();
1061 if (cur.pos() != 0 && cur.pos() != cur.lastpos()
1062 && cpar.isChangeEdited(0, cur.pos()))
1065 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
1066 LyXLayout_ptr const & layout = cpar.layout();
1068 // this is only allowed, if the current paragraph is not empty
1069 // or caption and if it has not the keepempty flag active
1070 if (cur.lastpos() == 0 && !cpar.allowEmpty()
1071 && layout->labeltype != LABEL_SENSITIVE)
1074 // a layout change may affect also the following paragraph
1075 recUndo(cur.par(), undoSpan(cur.par()) - 1);
1077 // Always break behind a space
1078 // It is better to erase the space (Dekel)
1079 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
1080 cpar.erase(cur.pos());
1082 // break the paragraph
1086 keep_layout = layout->isEnvironment();
1088 // we need to set this before we insert the paragraph. IMO the
1089 // breakParagraph call should return a bool if it inserts the
1090 // paragraph before or behind and we should react on that one
1091 // but we can fix this in 1.3.0 (Jug 20020509)
1092 bool const isempty = cpar.allowEmpty() && cpar.empty();
1093 ::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
1094 cur.pos(), keep_layout);
1097 par_type next_par = cpit + 1;
1099 // well this is the caption hack since one caption is really enough
1100 if (layout->labeltype == LABEL_SENSITIVE) {
1102 // set to standard-layout
1103 pars_[cpit].applyLayout(tclass.defaultLayout());
1105 // set to standard-layout
1106 pars_[next_par].applyLayout(tclass.defaultLayout());
1109 // if the cursor is at the beginning of a row without prior newline,
1111 // This touches only the screen-update. Otherwise we would may have
1112 // an empty row on the screen
1113 if (cur.pos() != 0 && cur.textRow().pos() == cur.pos()
1114 && !pars_[cpit].isNewline(cur.pos() - 1))
1119 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
1120 pars_[next_par].erase(0);
1123 redoParagraph(cpit);
1124 redoParagraph(next_par);
1126 // This check is necessary. Otherwise the new empty paragraph will
1127 // be deleted automatically. And it is more friendly for the user!
1128 if (cur.pos() != 0 || isempty)
1129 setCursor(cur, cur.par() + 1, 0);
1131 setCursor(cur, cur.par(), 0);
1135 // convenience function
1136 void LyXText::redoParagraph(LCursor & cur)
1138 BOOST_ASSERT(this == cur.text());
1139 cur.clearSelection();
1140 redoParagraph(cur.par());
1141 setCursorIntern(cur, cur.par(), cur.pos());
1145 // insert a character, moves all the following breaks in the
1146 // same Paragraph one to the right and make a rebreak
1147 void LyXText::insertChar(LCursor & cur, char c)
1149 BOOST_ASSERT(this == cur.text());
1150 BOOST_ASSERT(c != Paragraph::META_INSET);
1152 recordUndo(cur, Undo::INSERT);
1154 Paragraph & par = cur.paragraph();
1155 // try to remove this
1156 par_type pit = cur.par();
1158 bool const freeSpacing = par.layout()->free_spacing ||
1159 par.isFreeSpacing();
1161 if (lyxrc.auto_number) {
1162 static string const number_operators = "+-/*";
1163 static string const number_unary_operators = "+-";
1164 static string const number_seperators = ".,:";
1166 if (current_font.number() == LyXFont::ON) {
1167 if (!IsDigit(c) && !contains(number_operators, c) &&
1168 !(contains(number_seperators, c) &&
1170 cur.pos() != cur.lastpos() &&
1171 getFont(pit, cur.pos()).number() == LyXFont::ON &&
1172 getFont(pit, cur.pos() - 1).number() == LyXFont::ON)
1174 number(cur); // Set current_font.number to OFF
1175 } else if (IsDigit(c) &&
1176 real_current_font.isVisibleRightToLeft()) {
1177 number(cur); // Set current_font.number to ON
1179 if (cur.pos() != 0) {
1180 char const c = par.getChar(cur.pos() - 1);
1181 if (contains(number_unary_operators, c) &&
1183 || par.isSeparator(cur.pos() - 2)
1184 || par.isNewline(cur.pos() - 2))
1186 setCharFont(pit, cur.pos() - 1, current_font);
1187 } else if (contains(number_seperators, c)
1189 && getFont(pit, cur.pos() - 2).number() == LyXFont::ON) {
1190 setCharFont(pit, cur.pos() - 1, current_font);
1196 // First check, if there will be two blanks together or a blank at
1197 // the beginning of a paragraph.
1198 // I decided to handle blanks like normal characters, the main
1199 // difference are the special checks when calculating the row.fill
1200 // (blank does not count at the end of a row) and the check here
1202 // The bug is triggered when we type in a description environment:
1203 // The current_font is not changed when we go from label to main text
1204 // and it should (along with realtmpfont) when we type the space.
1205 // CHECK There is a bug here! (Asger)
1207 // store the current font. This is because of the use of cursor
1208 // movements. The moving cursor would refresh the current font
1209 LyXFont realtmpfont = real_current_font;
1210 LyXFont rawtmpfont = current_font;
1212 // When the free-spacing option is set for the current layout,
1213 // disable the double-space checking
1214 if (!freeSpacing && IsLineSeparatorChar(c)) {
1215 if (cur.pos() == 0) {
1216 static bool sent_space_message = false;
1217 if (!sent_space_message) {
1218 cur.message(_("You cannot insert a space at the "
1219 "beginning of a paragraph. Please read the Tutorial."));
1220 sent_space_message = true;
1224 BOOST_ASSERT(cur.pos() > 0);
1225 if (par.isLineSeparator(cur.pos() - 1)
1226 || par.isNewline(cur.pos() - 1)) {
1227 static bool sent_space_message = false;
1228 if (!sent_space_message) {
1229 cur.message(_("You cannot type two spaces this way. "
1230 "Please read the Tutorial."));
1231 sent_space_message = true;
1237 par.insertChar(cur.pos(), c, rawtmpfont);
1239 current_font = rawtmpfont;
1240 real_current_font = realtmpfont;
1242 setCursor(cur, cur.par(), cur.pos() + 1, false, cur.boundary());
1247 void LyXText::charInserted()
1249 // Here we call finishUndo for every 20 characters inserted.
1250 // This is from my experience how emacs does it. (Lgb)
1251 static unsigned int counter;
1261 RowMetrics LyXText::computeRowMetrics(par_type pit, Row const & row) const
1265 double w = width_ - row.width();
1267 bool const is_rtl = isRTL(pars_[pit]);
1269 result.x = rightMargin(pars_[pit]);
1271 result.x = leftMargin(pit, row.pos());
1273 // is there a manual margin with a manual label
1274 LyXLayout_ptr const & layout = pars_[pit].layout();
1276 if (layout->margintype == MARGIN_MANUAL
1277 && layout->labeltype == LABEL_MANUAL) {
1278 /// We might have real hfills in the label part
1279 int nlh = numberOfLabelHfills(pars_[pit], row);
1281 // A manual label par (e.g. List) has an auto-hfill
1282 // between the label text and the body of the
1284 // But we don't want to do this auto hfill if the par
1286 if (!pars_[pit].empty())
1289 if (nlh && !pars_[pit].getLabelWidthString().empty())
1290 result.label_hfill = labelFill(pit, row) / double(nlh);
1293 // are there any hfills in the row?
1294 int const nh = numberOfHfills(pars_[pit], row);
1298 result.hfill = w / nh;
1299 // we don't have to look at the alignment if it is ALIGN_LEFT and
1300 // if the row is already larger then the permitted width as then
1301 // we force the LEFT_ALIGN'edness!
1302 } else if (int(row.width()) < maxwidth_) {
1303 // is it block, flushleft or flushright?
1304 // set x how you need it
1306 if (pars_[pit].params().align() == LYX_ALIGN_LAYOUT)
1307 align = layout->align;
1309 align = pars_[pit].params().align();
1311 // Display-style insets should always be on a centred row
1312 // The test on pars_[pit].size() is to catch zero-size pars, which
1313 // would trigger the assert in Paragraph::getInset().
1314 //inset = pars_[pit].size() ? pars_[pit].getInset(row.pos()) : 0;
1315 if (!pars_[pit].empty()
1316 && pars_[pit].isInset(row.pos())
1317 && pars_[pit].getInset(row.pos())->display())
1319 align = LYX_ALIGN_CENTER;
1323 case LYX_ALIGN_BLOCK: {
1324 int const ns = numberOfSeparators(pars_[pit], row);
1325 bool disp_inset = false;
1326 if (row.endpos() < pars_[pit].size()) {
1327 InsetBase const * in = pars_[pit].getInset(row.endpos());
1329 disp_inset = in->display();
1331 // If we have separators, this is not the last row of a
1332 // par, does not end in newline, and is not row above a
1333 // display inset... then stretch it
1335 && row.endpos() < pars_[pit].size()
1336 && !pars_[pit].isNewline(row.endpos() - 1)
1339 result.separator = w / ns;
1340 } else if (is_rtl) {
1345 case LYX_ALIGN_RIGHT:
1348 case LYX_ALIGN_CENTER:
1354 bidi.computeTables(pars_[pit], *bv()->buffer(), row);
1356 pos_type body_pos = pars_[pit].beginOfBody();
1357 pos_type end = row.endpos();
1360 && (body_pos > end || !pars_[pit].isLineSeparator(body_pos - 1)))
1362 result.x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1363 if (body_pos <= end)
1364 result.x += result.label_hfill;
1372 // the cursor set functions have a special mechanism. When they
1373 // realize, that you left an empty paragraph, they will delete it.
1375 void LyXText::cursorRightOneWord(LCursor & cur)
1377 BOOST_ASSERT(this == cur.text());
1378 if (cur.pos() == cur.lastpos() && cur.par() != cur.lastpar()) {
1382 // Skip through initial nonword stuff.
1383 // Treat floats and insets as words.
1384 while (cur.pos() != cur.lastpos() && !cur.paragraph().isWord(cur.pos()))
1386 // Advance through word.
1387 while (cur.pos() != cur.lastpos() && cur.paragraph().isWord(cur.pos()))
1390 setCursor(cur, cur.par(), cur.pos());
1394 void LyXText::cursorLeftOneWord(LCursor & cur)
1396 BOOST_ASSERT(this == cur.text());
1397 if (cur.pos() == 0 && cur.par() != 0) {
1399 cur.pos() = cur.lastpos();
1401 // Skip through initial nonword stuff.
1402 // Treat floats and insets as words.
1403 while (cur.pos() != 0 && !cur.paragraph().isWord(cur.pos() - 1))
1405 // Advance through word.
1406 while (cur.pos() != 0 && cur.paragraph().isWord(cur.pos() - 1))
1409 setCursor(cur, cur.par(), cur.pos());
1413 void LyXText::selectWord(LCursor & cur, word_location loc)
1415 BOOST_ASSERT(this == cur.text());
1416 CursorSlice from = cur.top();
1417 CursorSlice to = cur.top();
1418 getWord(from, to, loc);
1419 if (cur.top() != from)
1420 setCursor(cur, from.par(), from.pos());
1424 setCursor(cur, to.par(), to.pos());
1429 // Select the word currently under the cursor when no
1430 // selection is currently set
1431 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1433 BOOST_ASSERT(this == cur.text());
1434 if (cur.selection())
1436 selectWord(cur, loc);
1437 return cur.selection();
1441 void LyXText::acceptChange(LCursor & cur)
1443 BOOST_ASSERT(this == cur.text());
1444 if (!cur.selection() && cur.lastpos() != 0)
1447 CursorSlice const & startc = cur.selBegin();
1448 CursorSlice const & endc = cur.selEnd();
1449 if (startc.par() == endc.par()) {
1450 recordUndoSelection(cur, Undo::INSERT);
1451 pars_[startc.par()].acceptChange(startc.pos(), endc.pos());
1453 cur.clearSelection();
1454 redoParagraph(startc.par());
1455 setCursorIntern(cur, startc.par(), 0);
1457 #ifdef WITH_WARNINGS
1458 #warning handle multi par selection
1463 void LyXText::rejectChange(LCursor & cur)
1465 BOOST_ASSERT(this == cur.text());
1466 if (!cur.selection() && cur.lastpos() != 0)
1469 CursorSlice const & startc = cur.selBegin();
1470 CursorSlice const & endc = cur.selEnd();
1471 if (startc.par() == endc.par()) {
1472 recordUndoSelection(cur, Undo::INSERT);
1473 pars_[startc.par()].rejectChange(startc.pos(), endc.pos());
1475 cur.clearSelection();
1476 redoParagraph(startc.par());
1477 setCursorIntern(cur, startc.par(), 0);
1479 #ifdef WITH_WARNINGS
1480 #warning handle multi par selection
1485 // Delete from cursor up to the end of the current or next word.
1486 void LyXText::deleteWordForward(LCursor & cur)
1488 BOOST_ASSERT(this == cur.text());
1489 if (cur.lastpos() == 0)
1493 cur.selection() = true;
1494 cursorRightOneWord(cur);
1496 cutSelection(cur, true, false);
1501 // Delete from cursor to start of current or prior word.
1502 void LyXText::deleteWordBackward(LCursor & cur)
1504 BOOST_ASSERT(this == cur.text());
1505 if (cur.lastpos() == 0)
1509 cur.selection() = true;
1510 cursorLeftOneWord(cur);
1512 cutSelection(cur, true, false);
1517 // Kill to end of line.
1518 void LyXText::deleteLineForward(LCursor & cur)
1520 BOOST_ASSERT(this == cur.text());
1521 if (cur.lastpos() == 0) {
1522 // Paragraph is empty, so we just go to the right
1526 cur.selection() = true; // to avoid deletion
1529 // What is this test for ??? (JMarc)
1530 if (!cur.selection())
1531 deleteWordForward(cur);
1533 cutSelection(cur, true, false);
1538 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1540 BOOST_ASSERT(this == cur.text());
1544 if (cur.selection()) {
1545 from = cur.selBegin();
1549 getWord(from, to, lyx::PARTIAL_WORD);
1550 setCursor(cur, to.par(), to.pos() + 1);
1553 recordUndoSelection(cur);
1555 pos_type pos = from.pos();
1556 int par = from.par();
1558 while (par != int(pars_.size()) && (pos != to.pos() || par != to.par())) {
1560 if (pos == pars_[pit].size()) {
1565 unsigned char c = pars_[pit].getChar(pos);
1566 if (c != Paragraph::META_INSET) {
1568 case text_lowercase:
1571 case text_capitalization:
1573 action = text_lowercase;
1575 case text_uppercase:
1580 #ifdef WITH_WARNINGS
1583 pars_[pit].setChar(pos, c);
1589 void LyXText::Delete(LCursor & cur)
1591 BOOST_ASSERT(this == cur.text());
1592 if (cur.pos() != cur.lastpos()) {
1593 recordUndo(cur, Undo::DELETE, cur.par());
1594 setCursorIntern(cur, cur.par(), cur.pos() + 1, false, cur.boundary());
1597 // should we do anything in an else branch?
1601 void LyXText::backspace(LCursor & cur)
1603 BOOST_ASSERT(this == cur.text());
1604 if (cur.pos() == 0) {
1605 // The cursor is at the beginning of a paragraph, so
1606 // the the backspace will collapse two paragraphs into
1609 // but it's not allowed unless it's new
1610 Paragraph & par = cur.paragraph();
1611 if (par.isChangeEdited(0, par.size()))
1614 // we may paste some paragraphs
1616 // is it an empty paragraph?
1617 pos_type lastpos = cur.lastpos();
1618 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1619 // This is an empty paragraph and we delete it just
1620 // by moving the cursor one step
1621 // left and let the DeleteEmptyParagraphMechanism
1622 // handle the actual deletion of the paragraph.
1624 if (cur.par() != 0) {
1626 // the layout things can change the height of a row !
1633 recordUndo(cur, Undo::DELETE, cur.par() - 1);
1635 par_type tmppit = cur.par();
1636 // We used to do cursorLeftIntern() here, but it is
1637 // not a good idea since it triggers the auto-delete
1638 // mechanism. So we do a cursorLeftIntern()-lite,
1639 // without the dreaded mechanism. (JMarc)
1640 if (cur.par() != 0) {
1641 // steps into the above paragraph.
1642 setCursorIntern(cur, cur.par() - 1,
1643 pars_[cur.par() - 1].size(),
1647 // Pasting is not allowed, if the paragraphs have different
1648 // layout. I think it is a real bug of all other
1649 // word processors to allow it. It confuses the user.
1650 // Correction: Pasting is always allowed with standard-layout
1651 Buffer & buf = cur.buffer();
1652 BufferParams const & bufparams = buf.params();
1653 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1654 par_type const cpit = cur.par();
1657 && (pars_[cpit].layout() == pars_[tmppit].layout()
1658 || pars_[tmppit].layout() == tclass.defaultLayout())
1659 && pars_[cpit].getAlign() == pars_[tmppit].getAlign()) {
1660 mergeParagraph(bufparams, buf.paragraphs(), cpit);
1662 if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
1665 // the counters may have changed
1667 setCursor(cur, cur.par(), cur.pos(), false);
1670 // this is the code for a normal backspace, not pasting
1672 recordUndo(cur, Undo::DELETE);
1673 // We used to do cursorLeftIntern() here, but it is
1674 // not a good idea since it triggers the auto-delete
1675 // mechanism. So we do a cursorLeftIntern()-lite,
1676 // without the dreaded mechanism. (JMarc)
1677 setCursorIntern(cur, cur.par(), cur.pos() - 1,
1678 false, cur.boundary());
1679 cur.paragraph().erase(cur.pos());
1682 if (cur.pos() == cur.lastpos())
1683 setCurrentFont(cur);
1686 setCursor(cur, cur.par(), cur.pos(), false, cur.boundary());
1690 Paragraph & LyXText::getPar(par_type par) const
1692 //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
1693 BOOST_ASSERT(par >= 0);
1694 BOOST_ASSERT(par < int(paragraphs().size()));
1695 return paragraphs()[par];
1699 // y is relative to this LyXText's top
1700 Row const & LyXText::getRowNearY(int y, par_type & pit) const
1702 BOOST_ASSERT(!paragraphs().empty());
1703 BOOST_ASSERT(!paragraphs().begin()->rows.empty());
1704 par_type const pend = paragraphs().size() - 1;
1706 while (int(pars_[pit].y + pars_[pit].height) < y && pit != pend)
1709 RowList::iterator rit = pars_[pit].rows.end();
1710 RowList::iterator const rbegin = pars_[pit].rows.begin();
1713 } while (rit != rbegin && int(pars_[pit].y + rit->y_offset()) > y);
1719 Row const & LyXText::firstRow() const
1721 return *paragraphs().front().rows.begin();
1725 void LyXText::redoParagraphInternal(par_type pit)
1727 // remove rows of paragraph, keep track of height changes
1728 height_ -= pars_[pit].height;
1731 pars_[pit].rows.clear();
1732 pars_[pit].height = 0;
1733 pars_[pit].width = 0;
1736 InsetList::iterator ii = pars_[pit].insetlist.begin();
1737 InsetList::iterator iend = pars_[pit].insetlist.end();
1738 for (; ii != iend; ++ii) {
1740 int const w = maxwidth_ - leftMargin(pit) - rightMargin(pars_[pit]);
1741 MetricsInfo mi(bv(), getFont(pit, ii->pos), w);
1742 ii->inset->metrics(mi, dim);
1745 // rebreak the paragraph
1746 pars_[pit].setBeginOfBody();
1750 rowBreakPoint(pit, row);
1751 setRowWidth(pit, row);
1752 setHeightOfRow(pit, row);
1753 row.y_offset(pars_[pit].height);
1754 pars_[pit].rows.push_back(row);
1755 pars_[pit].width = std::max(pars_[pit].width, row.width());
1756 pars_[pit].height += row.height();
1758 } while (z < pars_[pit].size());
1760 height_ += pars_[pit].height;
1761 //lyxerr << "redoParagraph: " << pars_[pit].rows.size() << " rows\n";
1765 void LyXText::redoParagraphs(par_type pit, par_type end)
1767 for (; pit != end; ++pit)
1768 redoParagraphInternal(pit);
1769 updateParPositions();
1774 void LyXText::redoParagraph(par_type pit)
1776 redoParagraphInternal(pit);
1777 updateParPositions();
1781 void LyXText::fullRebreak()
1783 redoParagraphs(0, paragraphs().size());
1784 bv()->cursor().resetAnchor();
1788 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1790 //BOOST_ASSERT(mi.base.textwidth);
1791 if (mi.base.textwidth)
1792 maxwidth_ = mi.base.textwidth;
1793 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1794 //<< " maxWidth: " << maxwidth << "\nfont: " << mi.base.font
1797 // Rebuild row cache. This recomputes height as well.
1798 redoParagraphs(0, paragraphs().size());
1800 width_ = maxParagraphWidth(paragraphs());
1803 dim.asc = firstRow().ascent_of_text();
1804 dim.des = height_ - dim.asc;
1809 // only used for inset right now. should also be used for main text
1810 void LyXText::draw(PainterInfo & pi, int x, int y) const
1814 paintTextInset(*this, pi);
1818 // only used for inset right now. should also be used for main text
1819 void LyXText::drawSelection(PainterInfo &, int, int) const
1821 //lyxerr << "LyXText::drawSelection at " << x << " " << y << endl;
1825 bool LyXText::isLastRow(par_type pit, Row const & row) const
1827 return row.endpos() >= pars_[pit].size()
1828 && pit + 1 == par_type(paragraphs().size());
1832 bool LyXText::isFirstRow(par_type pit, Row const & row) const
1834 return row.pos() == 0 && pit == 0;
1838 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1839 word_location const loc)
1841 Paragraph const & from_par = pars_[from.par()];
1843 case lyx::WHOLE_WORD_STRICT:
1844 if (from.pos() == 0 || from.pos() == from_par.size()
1845 || !from_par.isWord(from.pos())
1846 || !from_par.isWord(from.pos() - 1)) {
1850 // no break here, we go to the next
1852 case lyx::WHOLE_WORD:
1853 // If we are already at the beginning of a word, do nothing
1854 if (!from.pos() || !from_par.isWord(from.pos() - 1))
1856 // no break here, we go to the next
1858 case lyx::PREVIOUS_WORD:
1859 // always move the cursor to the beginning of previous word
1860 while (from.pos() && from_par.isWord(from.pos() - 1))
1863 case lyx::NEXT_WORD:
1864 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1867 case lyx::PARTIAL_WORD:
1868 // no need to move the 'from' cursor
1872 Paragraph & to_par = pars_[to.par()];
1873 while (to.pos() < to_par.size() && to_par.isWord(to.pos()))
1878 void LyXText::write(Buffer const & buf, std::ostream & os) const
1880 ParagraphList::const_iterator pit = paragraphs().begin();
1881 ParagraphList::const_iterator end = paragraphs().end();
1882 Paragraph::depth_type dth = 0;
1883 for (; pit != end; ++pit)
1884 pit->write(buf, os, buf.params(), dth);
1888 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1890 static Change current_change;
1892 bool the_end_read = false;
1893 Paragraph::depth_type depth = 0;
1895 while (lex.isOK()) {
1897 string const token = lex.getString();
1902 if (token == "\\end_inset") {
1903 the_end_read = true;
1907 if (token == "\\end_document") {
1908 #ifdef WITH_WARNINGS
1912 lex.printError("\\end_document read in inset! Error in document!");
1920 if (token == "\\begin_layout") {
1921 lex.pushToken(token);
1924 par.params().depth(depth);
1925 if (buf.params().tracking_changes)
1927 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1928 pars_.push_back(par);
1930 // FIXME: goddamn InsetTabular makes us pass a Buffer
1932 ::readParagraph(buf, pars_.back(), lex);
1934 } else if (token == "\\begin_deeper") {
1936 } else if (token == "\\end_deeper") {
1938 lex.printError("\\end_deeper: " "depth is already null");
1947 return the_end_read;
1951 int LyXText::ascent() const
1953 return firstRow().ascent_of_text();
1957 int LyXText::descent() const
1959 return height_ - firstRow().ascent_of_text();
1963 int LyXText::cursorX(CursorSlice const & cur) const
1965 par_type pit = cur.par();
1966 if (pars_[pit].rows.empty())
1969 Row const & row = *pars_[pit].getRow(cur.pos());
1971 pos_type pos = cur.pos();
1972 pos_type cursor_vpos = 0;
1974 RowMetrics const m = computeRowMetrics(pit, row);
1977 pos_type const row_pos = row.pos();
1978 pos_type const end = row.endpos();
1981 cursor_vpos = row_pos;
1982 else if (pos >= end)
1983 cursor_vpos = isRTL(pars_[pit]) ? row_pos : end;
1984 else if (pos > row_pos && pos >= end)
1985 // Place cursor after char at (logical) position pos - 1
1986 cursor_vpos = (bidi.level(pos - 1) % 2 == 0)
1987 ? bidi.log2vis(pos - 1) + 1 : bidi.log2vis(pos - 1);
1989 // Place cursor before char at (logical) position pos
1990 cursor_vpos = (bidi.level(pos) % 2 == 0)
1991 ? bidi.log2vis(pos) : bidi.log2vis(pos) + 1;
1993 pos_type body_pos = pars_[pit].beginOfBody();
1995 (body_pos > end || !pars_[pit].isLineSeparator(body_pos - 1)))
1998 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1999 pos_type pos = bidi.vis2log(vpos);
2000 if (body_pos > 0 && pos == body_pos - 1) {
2002 + font_metrics::width(pars_[pit].layout()->labelsep,
2004 if (pars_[pit].isLineSeparator(body_pos - 1))
2005 x -= singleWidth(pit, body_pos - 1);
2008 if (hfillExpansion(pars_[pit], row, pos)) {
2009 x += singleWidth(pit, pos);
2010 if (pos >= body_pos)
2014 } else if (pars_[pit].isSeparator(pos)) {
2015 x += singleWidth(pit, pos);
2016 if (pos >= body_pos)
2019 x += singleWidth(pit, pos);
2021 return xo_ + int(x);
2025 int LyXText::cursorY(CursorSlice const & cur) const
2027 Paragraph const & par = getPar(cur.par());
2028 Row const & row = *par.getRow(cur.pos());
2029 return yo_ + par.y + row.y_offset() + row.baseline();
2033 // Returns the current font and depth as a message.
2034 string LyXText::currentState(LCursor & cur)
2036 BOOST_ASSERT(this == cur.text());
2037 Buffer & buf = cur.buffer();
2038 Paragraph const & par = cur.paragraph();
2039 std::ostringstream os;
2041 bool const show_change = buf.params().tracking_changes
2042 && cur.pos() != cur.lastpos()
2043 && par.lookupChange(cur.pos()) != Change::UNCHANGED;
2046 Change change = par.lookupChangeFull(cur.pos());
2047 Author const & a = buf.params().authors().get(change.author);
2048 os << _("Change: ") << a.name();
2049 if (!a.email().empty())
2050 os << " (" << a.email() << ")";
2051 if (change.changetime)
2052 os << _(" at ") << ctime(&change.changetime);
2056 // I think we should only show changes from the default
2058 LyXFont font = real_current_font;
2059 font.reduce(buf.params().getLyXTextClass().defaultfont());
2061 // avoid _(...) re-entrance problem
2062 string const s = font.stateText(&buf.params());
2063 os << bformat(_("Font: %1$s"), s);
2065 // os << bformat(_("Font: %1$s"), font.stateText(&buf.params));
2067 // The paragraph depth
2068 int depth = cur.paragraph().getDepth();
2070 os << bformat(_(", Depth: %1$s"), tostr(depth));
2072 // The paragraph spacing, but only if different from
2074 Spacing const & spacing = par.params().spacing();
2075 if (!spacing.isDefault()) {
2076 os << _(", Spacing: ");
2077 switch (spacing.getSpace()) {
2078 case Spacing::Single:
2081 case Spacing::Onehalf:
2084 case Spacing::Double:
2087 case Spacing::Other:
2088 os << _("Other (") << spacing.getValue() << ')';
2090 case Spacing::Default:
2091 // should never happen, do nothing
2095 #ifdef DEVEL_VERSION
2096 os << _(", Inset: ") << &cur.inset();
2097 os << _(", Paragraph: ") << cur.par();
2098 os << _(", Id: ") << par.id();
2099 os << _(", Position: ") << cur.pos();
2100 Row & row = cur.textRow();
2101 os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2107 string LyXText::getPossibleLabel(LCursor & cur) const
2109 par_type pit = cur.par();
2111 LyXLayout_ptr layout = pars_[pit].layout();
2113 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2114 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
2115 if (layout2->latextype != LATEX_PARAGRAPH) {
2121 string text = layout->latexname().substr(0, 3);
2122 if (layout->latexname() == "theorem")
2123 text = "thm"; // Create a correct prefix for prettyref
2126 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2129 string par_text = pars_[pit].asString(cur.buffer(), false);
2130 for (int i = 0; i < lyxrc.label_init_length; ++i) {
2131 if (par_text.empty())
2134 par_text = split(par_text, head, ' ');
2135 // Is it legal to use spaces in labels ?
2145 int LyXText::dist(int x, int y) const
2152 else if (x > xo_ + int(width_))
2153 xx = x - xo_ - width_;
2155 if (y < yo_ - ascent())
2156 yy = yo_ - ascent() - y;
2157 else if (y > yo_ + descent())
2158 yy = y - yo_ - descent();