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;
95 int numberOfSeparators(Paragraph const & par, Row const & row)
97 pos_type const first = max(row.pos(), par.beginOfBody());
98 pos_type const last = row.endpos() - 1;
100 for (pos_type p = first; p < last; ++p) {
101 if (par.isSeparator(p))
108 unsigned int maxParagraphWidth(ParagraphList const & plist)
110 unsigned int width = 0;
111 ParagraphList::const_iterator pit = plist.begin();
112 ParagraphList::const_iterator end = plist.end();
113 for (; pit != end; ++pit)
114 width = std::max(width, pit->width);
119 int numberOfLabelHfills(Paragraph const & par, Row const & row)
121 pos_type last = row.endpos() - 1;
122 pos_type first = row.pos();
124 // hfill *DO* count at the beginning of paragraphs!
126 while (first < last && par.isHfill(first))
130 last = min(last, par.beginOfBody());
132 for (pos_type p = first; p < last; ++p) {
140 int numberOfHfills(Paragraph const & par, Row const & row)
142 pos_type const last = row.endpos() - 1;
143 pos_type first = row.pos();
145 // hfill *DO* count at the beginning of paragraphs!
147 while (first < last && par.isHfill(first))
151 first = max(first, par.beginOfBody());
154 for (pos_type p = first; p < last; ++p) {
162 int readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
163 string const & token)
166 static Change change;
168 BufferParams const & bp = buf.params();
170 if (token[0] != '\\') {
171 string::const_iterator cit = token.begin();
172 for (; cit != token.end(); ++cit) {
173 par.insertChar(par.size(), (*cit), font, change);
175 } else if (token == "\\begin_layout") {
177 string layoutname = lex.getString();
179 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
182 LyXTextClass const & tclass = bp.getLyXTextClass();
184 if (layoutname.empty()) {
185 layoutname = tclass.defaultLayoutName();
188 bool hasLayout = tclass.hasLayout(layoutname);
191 lyxerr << "Layout '" << layoutname << "' does not"
192 << " exist in textclass '" << tclass.name()
194 lyxerr << "Trying to use default layout instead."
196 layoutname = tclass.defaultLayoutName();
199 par.layout(bp.getLyXTextClass()[layoutname]);
201 // Test whether the layout is obsolete.
202 LyXLayout_ptr const & layout = par.layout();
203 if (!layout->obsoleted_by().empty())
204 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
206 par.params().read(lex);
208 } else if (token == "\\end_layout") {
209 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
210 << "Missing \\begin_layout?.\n";
211 } else if (token == "\\end_inset") {
212 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
213 << "Missing \\begin_inset?.\n";
214 } else if (token == "\\begin_inset") {
215 InsetBase * inset = readInset(lex, buf);
217 par.insertInset(par.size(), inset, font, change);
220 string line = lex.getString();
221 buf.error(ErrorItem(_("Unknown Inset"), line,
222 par.id(), 0, par.size()));
225 } else if (token == "\\family") {
227 font.setLyXFamily(lex.getString());
228 } else if (token == "\\series") {
230 font.setLyXSeries(lex.getString());
231 } else if (token == "\\shape") {
233 font.setLyXShape(lex.getString());
234 } else if (token == "\\size") {
236 font.setLyXSize(lex.getString());
237 } else if (token == "\\lang") {
239 string const tok = lex.getString();
240 Language const * lang = languages.getLanguage(tok);
242 font.setLanguage(lang);
244 font.setLanguage(bp.language);
245 lex.printError("Unknown language `$$Token'");
247 } else if (token == "\\numeric") {
249 font.setNumber(font.setLyXMisc(lex.getString()));
250 } else if (token == "\\emph") {
252 font.setEmph(font.setLyXMisc(lex.getString()));
253 } else if (token == "\\bar") {
255 string const tok = lex.getString();
258 font.setUnderbar(LyXFont::ON);
259 else if (tok == "no")
260 font.setUnderbar(LyXFont::OFF);
261 else if (tok == "default")
262 font.setUnderbar(LyXFont::INHERIT);
264 lex.printError("Unknown bar font flag "
266 } else if (token == "\\noun") {
268 font.setNoun(font.setLyXMisc(lex.getString()));
269 } else if (token == "\\color") {
271 font.setLyXColor(lex.getString());
272 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
274 // Insets don't make sense in a free-spacing context! ---Kayvan
275 if (par.isFreeSpacing()) {
276 if (token == "\\InsetSpace")
277 par.insertChar(par.size(), ' ', font, change);
278 else if (lex.isOK()) {
280 string const next_token = lex.getString();
281 if (next_token == "\\-")
282 par.insertChar(par.size(), '-', font, change);
284 lex.printError("Token `$$Token' "
286 "paragraph layout!");
290 auto_ptr<InsetBase> inset;
291 if (token == "\\SpecialChar" )
292 inset.reset(new InsetSpecialChar);
294 inset.reset(new InsetSpace);
295 inset->read(buf, lex);
296 par.insertInset(par.size(), inset.release(),
299 } else if (token == "\\i") {
300 auto_ptr<InsetBase> inset(new InsetLatexAccent);
301 inset->read(buf, lex);
302 par.insertInset(par.size(), inset.release(), font, change);
303 } else if (token == "\\backslash") {
304 par.insertChar(par.size(), '\\', font, change);
305 } else if (token == "\\newline") {
306 auto_ptr<InsetBase> inset(new InsetNewline);
307 inset->read(buf, lex);
308 par.insertInset(par.size(), inset.release(), font, change);
309 } else if (token == "\\LyXTable") {
310 auto_ptr<InsetBase> inset(new InsetTabular(buf));
311 inset->read(buf, lex);
312 par.insertInset(par.size(), inset.release(), font, change);
313 } else if (token == "\\bibitem") {
314 InsetCommandParams p("bibitem", "dummy");
315 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
316 inset->read(buf, lex);
317 par.insertInset(par.size(), inset.release(), font, change);
318 } else if (token == "\\hfill") {
319 par.insertInset(par.size(), new InsetHFill, font, change);
320 } else if (token == "\\lyxline") {
321 par.insertInset(par.size(), new InsetLine, font, change);
322 } else if (token == "\\newpage") {
323 par.insertInset(par.size(), new InsetPagebreak, font, change);
324 } else if (token == "\\change_unchanged") {
325 // Hack ! Needed for empty paragraphs :/
326 // FIXME: is it still ??
329 change = Change(Change::UNCHANGED);
330 } else if (token == "\\change_inserted") {
332 std::istringstream is(lex.getString());
336 change = Change(Change::INSERTED, bp.author_map[aid], ct);
337 } else if (token == "\\change_deleted") {
339 std::istringstream is(lex.getString());
343 change = Change(Change::DELETED, bp.author_map[aid], ct);
346 buf.error(ErrorItem(_("Unknown token"),
347 bformat(_("Unknown token: %1$s %2$s\n"), token, lex.getString()),
348 par.id(), 0, par.size()));
355 int readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex)
360 string token = lex.getString();
364 unknown += readParToken(buf, par, lex, token);
367 token = lex.getString();
372 if (token == "\\end_layout") {
373 //Ok, paragraph finished
377 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
378 << token << '\'' << endl;
379 if (token == "\\begin_layout" || token == "\\end_document"
380 || token == "\\end_inset" || token == "\\begin_deeper"
381 || token == "\\end_deeper") {
382 lex.pushToken(token);
383 lyxerr << "Paragraph ended in line "
384 << lex.getLineNo() << "\n"
385 << "Missing \\end_layout.\n";
397 BufferView * LyXText::bv()
399 BOOST_ASSERT(bv_owner != 0);
404 BufferView * LyXText::bv() const
406 BOOST_ASSERT(bv_owner != 0);
411 double LyXText::spacing(Paragraph const & par) const
413 if (par.params().spacing().isDefault())
414 return bv()->buffer()->params().spacing().getValue();
415 return par.params().spacing().getValue();
419 void LyXText::updateParPositions()
422 par_type end = pars_.size();
423 for (height_ = 0; pit != end; ++pit) {
424 pars_[pit].y = height_;
425 height_ += pars_[pit].height;
430 int LyXText::width() const
436 int LyXText::height() const
442 int LyXText::singleWidth(Paragraph const & par, pos_type pos) const
444 return singleWidth(par, pos, par.getChar(pos), getFont(par, pos));
448 int LyXText::singleWidth(Paragraph const & par,
449 pos_type pos, char c, LyXFont const & font) const
451 if (pos >= par.size()) {
452 lyxerr << "in singleWidth(), pos: " << pos << endl;
457 // The most common case is handled first (Asger)
458 if (IsPrintable(c)) {
459 if (!font.language()->RightToLeft()) {
460 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
461 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
462 && font.language()->lang() == "arabic") {
463 if (Encodings::IsComposeChar_arabic(c))
466 c = par.transformChar(c, pos);
467 } else if (font.language()->lang() == "hebrew" &&
468 Encodings::IsComposeChar_hebrew(c))
471 return font_metrics::width(c, font);
474 if (c == Paragraph::META_INSET)
475 return par.getInset(pos)->width();
477 if (IsSeparatorChar(c))
479 return font_metrics::width(c, font);
483 int LyXText::leftMargin(par_type pit) const
485 return leftMargin(pit, pars_[pit].size());
489 int LyXText::leftMargin(par_type const pit, pos_type const pos) const
491 Paragraph const & par = pars_[pit];
492 LyXTextClass const & tclass =
493 bv()->buffer()->params().getLyXTextClass();
494 LyXLayout_ptr const & layout = par.layout();
496 string parindent = layout->parindent;
501 l_margin += changebarMargin();
503 l_margin += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
505 if (par.getDepth() != 0) {
506 // find the next level paragraph
507 par_type newpar = outerHook(pit, pars_);
508 if (newpar != par_type(pars_.size())) {
509 if (pars_[newpar].layout()->isEnvironment()) {
510 l_margin = leftMargin(newpar);
512 if (par.layout() == tclass.defaultLayout()) {
513 if (pars_[newpar].params().noindent())
516 parindent = pars_[newpar].layout()->parindent;
521 LyXFont const labelfont = getLabelFont(par);
522 switch (layout->margintype) {
524 if (!layout->leftmargin.empty())
525 l_margin += font_metrics::signedWidth(layout->leftmargin,
526 tclass.defaultfont());
527 if (!par.getLabelstring().empty()) {
528 l_margin += font_metrics::signedWidth(layout->labelindent,
530 l_margin += font_metrics::width(par.getLabelstring(),
532 l_margin += font_metrics::width(layout->labelsep, labelfont);
537 l_margin += font_metrics::signedWidth(layout->labelindent, labelfont);
538 // The width of an empty par, even with manual label, should be 0
539 if (!par.empty() && pos >= par.beginOfBody()) {
540 if (!par.getLabelWidthString().empty()) {
541 l_margin += font_metrics::width(par.getLabelWidthString(),
543 l_margin += font_metrics::width(layout->labelsep, labelfont);
549 l_margin += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
550 / (par.getDepth() + 4);
553 case MARGIN_FIRST_DYNAMIC:
554 if (layout->labeltype == LABEL_MANUAL) {
555 if (pos >= par.beginOfBody()) {
556 l_margin += font_metrics::signedWidth(layout->leftmargin,
559 l_margin += font_metrics::signedWidth(layout->labelindent,
563 // Special case to fix problems with
565 || (layout->labeltype == LABEL_STATIC
566 && layout->latextype == LATEX_ENVIRONMENT
567 && !isFirstInSequence(pit, pars_))) {
568 l_margin += font_metrics::signedWidth(layout->leftmargin,
570 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
571 && layout->labeltype != LABEL_BIBLIO
572 && layout->labeltype !=
573 LABEL_CENTERED_TOP_ENVIRONMENT) {
574 l_margin += font_metrics::signedWidth(layout->labelindent,
576 l_margin += font_metrics::width(layout->labelsep, labelfont);
577 l_margin += font_metrics::width(par.getLabelstring(),
582 case MARGIN_RIGHT_ADDRESS_BOX: {
584 // ok, a terrible hack. The left margin depends on the widest
585 // row in this paragraph.
586 RowList::iterator rit = par.rows.begin();
587 RowList::iterator end = par.rows.end();
589 #warning This is wrong.
591 int minfill = maxwidth_;
592 for ( ; rit != end; ++rit)
593 if (rit->fill() < minfill)
594 minfill = rit->fill();
595 l_margin += font_metrics::signedWidth(layout->leftmargin,
596 tclass.defaultfont());
599 // also wrong, but much shorter.
600 l_margin += maxwidth_ / 2;
605 if (!par.params().leftIndent().zero())
606 l_margin += par.params().leftIndent().inPixels(maxwidth_);
610 if (par.params().align() == LYX_ALIGN_LAYOUT)
611 align = layout->align;
613 align = par.params().align();
615 // set the correct parindent
617 && (layout->labeltype == LABEL_NO_LABEL
618 || layout->labeltype == LABEL_TOP_ENVIRONMENT
619 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
620 || (layout->labeltype == LABEL_STATIC
621 && layout->latextype == LATEX_ENVIRONMENT
622 && !isFirstInSequence(pit, pars_)))
623 && align == LYX_ALIGN_BLOCK
624 && !par.params().noindent()
625 // in tabulars and ert paragraphs are never indented!
626 && (par.ownerCode() != InsetBase::TEXT_CODE
627 && par.ownerCode() != InsetBase::ERT_CODE)
628 && (par.layout() != tclass.defaultLayout()
629 || bv()->buffer()->params().paragraph_separation ==
630 BufferParams::PARSEP_INDENT))
632 l_margin += font_metrics::signedWidth(parindent, tclass.defaultfont());
639 int LyXText::rightMargin(Paragraph const & par) const
641 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
643 // We do not want rightmargins on inner texts.
644 if (bv()->text() != this)
649 + font_metrics::signedWidth(tclass.rightmargin(),
650 tclass.defaultfont())
651 + font_metrics::signedWidth(par.layout()->rightmargin,
652 tclass.defaultfont())
653 * 4 / (par.getDepth() + 4);
660 int LyXText::labelEnd(par_type const pit) const
662 // labelEnd is only needed if the layout fills a flushleft label.
663 if (pars_[pit].layout()->margintype != MARGIN_MANUAL)
665 // return the beginning of the body
666 return leftMargin(pit);
672 // this needs special handling - only newlines count as a break point
673 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
675 pos_type const end = par.size();
678 if (par.isNewline(i))
687 void LyXText::rowBreakPoint(par_type const pit, Row & row) const
689 Paragraph const & par = pars_[pit];
690 pos_type const end = par.size();
691 pos_type const pos = row.pos();
697 // maximum pixel width of a row
698 int width = maxwidth_ - rightMargin(par); // - leftMargin(pit, row);
704 LyXLayout_ptr const & layout = par.layout();
706 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
707 row.endpos(addressBreakPoint(pos, par));
711 pos_type const body_pos = par.beginOfBody();
714 // Now we iterate through until we reach the right margin
715 // or the end of the par, then choose the possible break
718 int const left = leftMargin(pit, pos);
721 // pixel width since last breakpoint
724 FontIterator fi = FontIterator(*this, pit, pos);
725 pos_type point = end;
727 for ( ; i < end; ++i, ++fi) {
728 char const c = par.getChar(i);
731 int thiswidth = singleWidth(par, i, c, *fi);
733 // add the auto-hfill from label end to the body
734 if (body_pos && i == body_pos) {
735 int add = font_metrics::width(layout->labelsep, getLabelFont(par));
736 if (par.isLineSeparator(i - 1))
737 add -= singleWidth(par, i - 1);
739 add = std::max(add, labelEnd(pit) - x);
744 chunkwidth += thiswidth;
747 // break before a character that will fall off
748 // the right of the row
750 // if no break before, break here
751 if (point == end || chunkwidth >= width - left) {
758 // exit on last registered breakpoint:
762 if (par.isNewline(i)) {
768 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
773 if (par.isInset(i) && par.getInset(i)->display()) {
779 if (!par.isInset(i) || par.getInset(i)->isChar()) {
780 // some insets are line separators too
781 if (par.isLineSeparator(i)) {
782 // register breakpoint:
789 // maybe found one, but the par is short enough.
790 if (i == end && x < width)
793 // manual labels cannot be broken in LaTeX. But we
794 // want to make our on-screen rendering of footnotes
796 if (body_pos && point < body_pos)
803 void LyXText::setRowWidth(par_type const pit, Row & row) const
805 // get the pure distance
806 pos_type const end = row.endpos();
808 Paragraph const & par = pars_[pit];
809 string labelsep = par.layout()->labelsep;
810 int w = leftMargin(pit, row.pos());
812 pos_type const body_pos = par.beginOfBody();
813 pos_type i = row.pos();
816 FontIterator fi = FontIterator(*this, pit, i);
817 for ( ; i < end; ++i, ++fi) {
818 if (body_pos > 0 && i == body_pos) {
819 w += font_metrics::width(labelsep, getLabelFont(par));
820 if (par.isLineSeparator(i - 1))
821 w -= singleWidth(par, i - 1);
822 w = max(w, labelEnd(pit));
824 char const c = par.getChar(i);
825 w += singleWidth(par, i, c, *fi);
829 if (body_pos > 0 && body_pos >= end) {
830 w += font_metrics::width(labelsep, getLabelFont(par));
831 if (end > 0 && par.isLineSeparator(end - 1))
832 w -= singleWidth(par, end - 1);
833 w = max(w, labelEnd(pit));
836 row.width(w + rightMargin(par));
840 // returns the minimum space a manual label needs on the screen in pixel
841 int LyXText::labelFill(Paragraph const & par, Row const & row) const
843 pos_type last = par.beginOfBody();
845 BOOST_ASSERT(last > 0);
847 // -1 because a label ends with a space that is in the label
850 // a separator at this end does not count
851 if (par.isLineSeparator(last))
855 for (pos_type i = row.pos(); i <= last; ++i)
856 w += singleWidth(par, i);
858 string const & label = par.params().labelWidthString();
862 return max(0, font_metrics::width(label, getLabelFont(par)) - w);
866 LColor_color LyXText::backgroundColor() const
868 return LColor_color(LColor::color(background_color_));
872 void LyXText::setHeightOfRow(par_type const pit, Row & row)
874 Paragraph const & par = pars_[pit];
875 // get the maximum ascent and the maximum descent
876 double layoutasc = 0;
877 double layoutdesc = 0;
878 double const dh = defaultRowHeight();
880 // ok, let us initialize the maxasc and maxdesc value.
881 // Only the fontsize count. The other properties
882 // are taken from the layoutfont. Nicer on the screen :)
883 LyXLayout_ptr const & layout = par.layout();
885 // as max get the first character of this row then it can
886 // increase but not decrease the height. Just some point to
887 // start with so we don't have to do the assignment below too
889 LyXFont font = getFont(par, row.pos());
890 LyXFont::FONT_SIZE const tmpsize = font.size();
891 font = getLayoutFont(pit);
892 LyXFont::FONT_SIZE const size = font.size();
893 font.setSize(tmpsize);
895 LyXFont labelfont = getLabelFont(par);
897 // these are minimum values
898 double const spacing_val = layout->spacing.getValue() * spacing(par);
899 //lyxerr << "spacing_val = " << spacing_val << endl;
900 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
901 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
903 // insets may be taller
904 InsetList::const_iterator ii = par.insetlist.begin();
905 InsetList::const_iterator iend = par.insetlist.end();
906 for ( ; ii != iend; ++ii) {
907 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
908 maxasc = max(maxasc, ii->inset->ascent());
909 maxdesc = max(maxdesc, ii->inset->descent());
913 // Check if any custom fonts are larger (Asger)
914 // This is not completely correct, but we can live with the small,
915 // cosmetic error for now.
917 pos_type const pos_end = row.endpos();
919 LyXFont::FONT_SIZE maxsize =
920 par.highestFontInRange(row.pos(), pos_end, size);
921 if (maxsize > font.size()) {
922 font.setSize(maxsize);
923 maxasc = max(maxasc, font_metrics::maxAscent(font));
924 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
927 // This is nicer with box insets:
931 row.ascent_of_text(maxasc);
934 if (row.pos() == 0) {
935 BufferParams const & bufparams = bv()->buffer()->params();
936 // some parksips VERY EASY IMPLEMENTATION
937 if (bv()->buffer()->params().paragraph_separation
938 == BufferParams::PARSEP_SKIP
940 && ((layout->isParagraph() && par.getDepth() == 0)
941 || (pars_[pit - 1].layout()->isParagraph()
942 && pars_[pit - 1].getDepth() == 0)))
944 maxasc += bufparams.getDefSkip().inPixels(*bv());
947 if (pars_[pit].params().startOfAppendix())
948 maxasc += int(3 * dh);
950 // This is special code for the chapter, since the label of this
951 // layout is printed in an extra row
952 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
953 labeladdon = int(font_metrics::maxHeight(labelfont)
954 * layout->spacing.getValue()
958 // special code for the top label
959 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
960 || layout->labeltype == LABEL_BIBLIO
961 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
962 && isFirstInSequence(pit, paragraphs())
963 && !par.getLabelstring().empty())
966 font_metrics::maxHeight(labelfont)
967 * layout->spacing.getValue()
969 + (layout->topsep + layout->labelbottomsep) * dh);
972 // Add the layout spaces, for example before and after
973 // a section, or between the items of a itemize or enumerate
976 par_type prev = depthHook(pit, pars_, par.getDepth());
978 && pars_[prev].layout() == layout
979 && pars_[prev].getDepth() == par.getDepth()
980 && pars_[prev].getLabelWidthString() == par.getLabelWidthString())
982 layoutasc = layout->itemsep * dh;
983 } else if (pit != 0 || row.pos() != 0) {
984 if (layout->topsep > 0)
985 layoutasc = layout->topsep * dh;
988 prev = outerHook(pit, pars_);
989 if (prev != par_type(pars_.size())) {
990 maxasc += int(pars_[prev].layout()->parsep * dh);
991 } else if (pit != 0) {
992 if (pars_[pit - 1].getDepth() != 0 ||
993 pars_[pit - 1].layout() == layout) {
994 maxasc += int(layout->parsep * dh);
999 // is it a bottom line?
1000 if (row.endpos() >= par.size()) {
1001 // add the layout spaces, for example before and after
1002 // a section, or between the items of a itemize or enumerate
1004 par_type nextpit = pit + 1;
1005 if (nextpit != par_type(pars_.size())) {
1006 par_type cpit = pit;
1010 if (pars_[cpit].getDepth() > pars_[nextpit].getDepth()) {
1011 usual = pars_[cpit].layout()->bottomsep * dh;
1012 cpit = depthHook(cpit, paragraphs(), pars_[nextpit].getDepth());
1013 if (pars_[cpit].layout() != pars_[nextpit].layout()
1014 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1016 unusual = pars_[cpit].layout()->bottomsep * dh;
1018 layoutdesc = max(unusual, usual);
1019 } else if (pars_[cpit].getDepth() == pars_[nextpit].getDepth()) {
1020 if (pars_[cpit].layout() != pars_[nextpit].layout()
1021 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1022 layoutdesc = int(pars_[cpit].layout()->bottomsep * dh);
1027 // incalculate the layout spaces
1028 maxasc += int(layoutasc * 2 / (2 + pars_[pit].getDepth()));
1029 maxdesc += int(layoutdesc * 2 / (2 + pars_[pit].getDepth()));
1031 row.height(maxasc + maxdesc + labeladdon);
1032 row.baseline(maxasc + labeladdon);
1033 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
1041 void LyXText::breakParagraph(LCursor & cur, char keep_layout)
1043 BOOST_ASSERT(this == cur.text());
1044 // allow only if at start or end, or all previous is new text
1045 Paragraph & cpar = cur.paragraph();
1046 par_type cpit = cur.par();
1048 if (cur.pos() != 0 && cur.pos() != cur.lastpos()
1049 && cpar.isChangeEdited(0, cur.pos()))
1052 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
1053 LyXLayout_ptr const & layout = cpar.layout();
1055 // this is only allowed, if the current paragraph is not empty
1056 // or caption and if it has not the keepempty flag active
1057 if (cur.lastpos() == 0 && !cpar.allowEmpty()
1058 && layout->labeltype != LABEL_SENSITIVE)
1061 // a layout change may affect also the following paragraph
1062 recUndo(cur.par(), undoSpan(cur.par()) - 1);
1064 // Always break behind a space
1065 // It is better to erase the space (Dekel)
1066 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
1067 cpar.erase(cur.pos());
1069 // break the paragraph
1073 keep_layout = layout->isEnvironment();
1075 // we need to set this before we insert the paragraph. IMO the
1076 // breakParagraph call should return a bool if it inserts the
1077 // paragraph before or behind and we should react on that one
1078 // but we can fix this in 1.3.0 (Jug 20020509)
1079 bool const isempty = cpar.allowEmpty() && cpar.empty();
1080 ::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
1081 cur.pos(), keep_layout);
1084 par_type next_par = cpit + 1;
1086 // well this is the caption hack since one caption is really enough
1087 if (layout->labeltype == LABEL_SENSITIVE) {
1089 // set to standard-layout
1090 pars_[cpit].applyLayout(tclass.defaultLayout());
1092 // set to standard-layout
1093 pars_[next_par].applyLayout(tclass.defaultLayout());
1096 // if the cursor is at the beginning of a row without prior newline,
1098 // This touches only the screen-update. Otherwise we would may have
1099 // an empty row on the screen
1100 if (cur.pos() != 0 && cur.textRow().pos() == cur.pos()
1101 && !pars_[cpit].isNewline(cur.pos() - 1))
1106 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
1107 pars_[next_par].erase(0);
1110 redoParagraph(cpit);
1111 redoParagraph(next_par);
1113 // This check is necessary. Otherwise the new empty paragraph will
1114 // be deleted automatically. And it is more friendly for the user!
1115 if (cur.pos() != 0 || isempty)
1116 setCursor(cur, cur.par() + 1, 0);
1118 setCursor(cur, cur.par(), 0);
1122 // convenience function
1123 void LyXText::redoParagraph(LCursor & cur)
1125 BOOST_ASSERT(this == cur.text());
1126 cur.clearSelection();
1127 redoParagraph(cur.par());
1128 setCursorIntern(cur, cur.par(), cur.pos());
1132 // insert a character, moves all the following breaks in the
1133 // same Paragraph one to the right and make a rebreak
1134 void LyXText::insertChar(LCursor & cur, char c)
1136 BOOST_ASSERT(this == cur.text());
1137 BOOST_ASSERT(c != Paragraph::META_INSET);
1139 recordUndo(cur, Undo::INSERT);
1141 Paragraph & par = cur.paragraph();
1142 // try to remove this
1143 par_type const pit = cur.par();
1145 bool const freeSpacing = par.layout()->free_spacing ||
1146 par.isFreeSpacing();
1148 if (lyxrc.auto_number) {
1149 static string const number_operators = "+-/*";
1150 static string const number_unary_operators = "+-";
1151 static string const number_seperators = ".,:";
1153 if (current_font.number() == LyXFont::ON) {
1154 if (!IsDigit(c) && !contains(number_operators, c) &&
1155 !(contains(number_seperators, c) &&
1157 cur.pos() != cur.lastpos() &&
1158 getFont(par, cur.pos()).number() == LyXFont::ON &&
1159 getFont(par, cur.pos() - 1).number() == LyXFont::ON)
1161 number(cur); // Set current_font.number to OFF
1162 } else if (IsDigit(c) &&
1163 real_current_font.isVisibleRightToLeft()) {
1164 number(cur); // Set current_font.number to ON
1166 if (cur.pos() != 0) {
1167 char const c = par.getChar(cur.pos() - 1);
1168 if (contains(number_unary_operators, c) &&
1170 || par.isSeparator(cur.pos() - 2)
1171 || par.isNewline(cur.pos() - 2))
1173 setCharFont(pit, cur.pos() - 1, current_font);
1174 } else if (contains(number_seperators, c)
1176 && getFont(par, cur.pos() - 2).number() == LyXFont::ON) {
1177 setCharFont(pit, cur.pos() - 1, current_font);
1183 // First check, if there will be two blanks together or a blank at
1184 // the beginning of a paragraph.
1185 // I decided to handle blanks like normal characters, the main
1186 // difference are the special checks when calculating the row.fill
1187 // (blank does not count at the end of a row) and the check here
1189 // The bug is triggered when we type in a description environment:
1190 // The current_font is not changed when we go from label to main text
1191 // and it should (along with realtmpfont) when we type the space.
1192 // CHECK There is a bug here! (Asger)
1194 // store the current font. This is because of the use of cursor
1195 // movements. The moving cursor would refresh the current font
1196 LyXFont realtmpfont = real_current_font;
1197 LyXFont rawtmpfont = current_font;
1199 // When the free-spacing option is set for the current layout,
1200 // disable the double-space checking
1201 if (!freeSpacing && IsLineSeparatorChar(c)) {
1202 if (cur.pos() == 0) {
1203 static bool sent_space_message = false;
1204 if (!sent_space_message) {
1205 cur.message(_("You cannot insert a space at the "
1206 "beginning of a paragraph. Please read the Tutorial."));
1207 sent_space_message = true;
1211 BOOST_ASSERT(cur.pos() > 0);
1212 if (par.isLineSeparator(cur.pos() - 1)
1213 || par.isNewline(cur.pos() - 1)) {
1214 static bool sent_space_message = false;
1215 if (!sent_space_message) {
1216 cur.message(_("You cannot type two spaces this way. "
1217 "Please read the Tutorial."));
1218 sent_space_message = true;
1224 par.insertChar(cur.pos(), c, rawtmpfont);
1226 current_font = rawtmpfont;
1227 real_current_font = realtmpfont;
1229 setCursor(cur, cur.par(), cur.pos() + 1, false, cur.boundary());
1234 void LyXText::charInserted()
1236 // Here we call finishUndo for every 20 characters inserted.
1237 // This is from my experience how emacs does it. (Lgb)
1238 static unsigned int counter;
1248 RowMetrics LyXText::computeRowMetrics(par_type const pit, Row const & row) const
1251 Paragraph const & par = pars_[pit];
1253 double w = width_ - row.width();
1255 bool const is_rtl = isRTL(par);
1257 result.x = rightMargin(par);
1259 result.x = leftMargin(pit, row.pos());
1261 // is there a manual margin with a manual label
1262 LyXLayout_ptr const & layout = par.layout();
1264 if (layout->margintype == MARGIN_MANUAL
1265 && layout->labeltype == LABEL_MANUAL) {
1266 /// We might have real hfills in the label part
1267 int nlh = numberOfLabelHfills(par, row);
1269 // A manual label par (e.g. List) has an auto-hfill
1270 // between the label text and the body of the
1272 // But we don't want to do this auto hfill if the par
1277 if (nlh && !par.getLabelWidthString().empty())
1278 result.label_hfill = labelFill(par, row) / double(nlh);
1281 // are there any hfills in the row?
1282 int const nh = numberOfHfills(par, row);
1286 result.hfill = w / nh;
1287 // we don't have to look at the alignment if it is ALIGN_LEFT and
1288 // if the row is already larger then the permitted width as then
1289 // we force the LEFT_ALIGN'edness!
1290 } else if (int(row.width()) < maxwidth_) {
1291 // is it block, flushleft or flushright?
1292 // set x how you need it
1294 if (par.params().align() == LYX_ALIGN_LAYOUT)
1295 align = layout->align;
1297 align = par.params().align();
1299 // Display-style insets should always be on a centred row
1300 // The test on par.size() is to catch zero-size pars, which
1301 // would trigger the assert in Paragraph::getInset().
1302 //inset = par.size() ? par.getInset(row.pos()) : 0;
1304 && par.isInset(row.pos())
1305 && par.getInset(row.pos())->display())
1307 align = LYX_ALIGN_CENTER;
1311 case LYX_ALIGN_BLOCK: {
1312 int const ns = numberOfSeparators(par, row);
1313 bool disp_inset = false;
1314 if (row.endpos() < par.size()) {
1315 InsetBase const * in = par.getInset(row.endpos());
1317 disp_inset = in->display();
1319 // If we have separators, this is not the last row of a
1320 // par, does not end in newline, and is not row above a
1321 // display inset... then stretch it
1323 && row.endpos() < par.size()
1324 && !par.isNewline(row.endpos() - 1)
1327 result.separator = w / ns;
1328 } else if (is_rtl) {
1333 case LYX_ALIGN_RIGHT:
1336 case LYX_ALIGN_CENTER:
1342 bidi.computeTables(par, *bv()->buffer(), row);
1344 pos_type body_pos = par.beginOfBody();
1345 pos_type end = row.endpos();
1348 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1350 result.x += font_metrics::width(layout->labelsep, getLabelFont(par));
1351 if (body_pos <= end)
1352 result.x += result.label_hfill;
1360 // the cursor set functions have a special mechanism. When they
1361 // realize, that you left an empty paragraph, they will delete it.
1363 void LyXText::cursorRightOneWord(LCursor & cur)
1365 BOOST_ASSERT(this == cur.text());
1366 if (cur.pos() == cur.lastpos() && cur.par() != cur.lastpar()) {
1370 // Skip through initial nonword stuff.
1371 // Treat floats and insets as words.
1372 while (cur.pos() != cur.lastpos() && !cur.paragraph().isWord(cur.pos()))
1374 // Advance through word.
1375 while (cur.pos() != cur.lastpos() && cur.paragraph().isWord(cur.pos()))
1378 setCursor(cur, cur.par(), cur.pos());
1382 void LyXText::cursorLeftOneWord(LCursor & cur)
1384 BOOST_ASSERT(this == cur.text());
1385 if (cur.pos() == 0 && cur.par() != 0) {
1387 cur.pos() = cur.lastpos();
1389 // Skip through initial nonword stuff.
1390 // Treat floats and insets as words.
1391 while (cur.pos() != 0 && !cur.paragraph().isWord(cur.pos() - 1))
1393 // Advance through word.
1394 while (cur.pos() != 0 && cur.paragraph().isWord(cur.pos() - 1))
1397 setCursor(cur, cur.par(), cur.pos());
1401 void LyXText::selectWord(LCursor & cur, word_location loc)
1403 BOOST_ASSERT(this == cur.text());
1404 CursorSlice from = cur.top();
1405 CursorSlice to = cur.top();
1406 getWord(from, to, loc);
1407 if (cur.top() != from)
1408 setCursor(cur, from.par(), from.pos());
1412 setCursor(cur, to.par(), to.pos());
1417 // Select the word currently under the cursor when no
1418 // selection is currently set
1419 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1421 BOOST_ASSERT(this == cur.text());
1422 if (cur.selection())
1424 selectWord(cur, loc);
1425 return cur.selection();
1429 void LyXText::acceptChange(LCursor & cur)
1431 BOOST_ASSERT(this == cur.text());
1432 if (!cur.selection() && cur.lastpos() != 0)
1435 CursorSlice const & startc = cur.selBegin();
1436 CursorSlice const & endc = cur.selEnd();
1437 if (startc.par() == endc.par()) {
1438 recordUndoSelection(cur, Undo::INSERT);
1439 pars_[startc.par()].acceptChange(startc.pos(), endc.pos());
1441 cur.clearSelection();
1442 redoParagraph(startc.par());
1443 setCursorIntern(cur, startc.par(), 0);
1445 #ifdef WITH_WARNINGS
1446 #warning handle multi par selection
1451 void LyXText::rejectChange(LCursor & cur)
1453 BOOST_ASSERT(this == cur.text());
1454 if (!cur.selection() && cur.lastpos() != 0)
1457 CursorSlice const & startc = cur.selBegin();
1458 CursorSlice const & endc = cur.selEnd();
1459 if (startc.par() == endc.par()) {
1460 recordUndoSelection(cur, Undo::INSERT);
1461 pars_[startc.par()].rejectChange(startc.pos(), endc.pos());
1463 cur.clearSelection();
1464 redoParagraph(startc.par());
1465 setCursorIntern(cur, startc.par(), 0);
1467 #ifdef WITH_WARNINGS
1468 #warning handle multi par selection
1473 // Delete from cursor up to the end of the current or next word.
1474 void LyXText::deleteWordForward(LCursor & cur)
1476 BOOST_ASSERT(this == cur.text());
1477 if (cur.lastpos() == 0)
1481 cur.selection() = true;
1482 cursorRightOneWord(cur);
1484 cutSelection(cur, true, false);
1489 // Delete from cursor to start of current or prior word.
1490 void LyXText::deleteWordBackward(LCursor & cur)
1492 BOOST_ASSERT(this == cur.text());
1493 if (cur.lastpos() == 0)
1497 cur.selection() = true;
1498 cursorLeftOneWord(cur);
1500 cutSelection(cur, true, false);
1505 // Kill to end of line.
1506 void LyXText::deleteLineForward(LCursor & cur)
1508 BOOST_ASSERT(this == cur.text());
1509 if (cur.lastpos() == 0) {
1510 // Paragraph is empty, so we just go to the right
1514 cur.selection() = true; // to avoid deletion
1517 // What is this test for ??? (JMarc)
1518 if (!cur.selection())
1519 deleteWordForward(cur);
1521 cutSelection(cur, true, false);
1526 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1528 BOOST_ASSERT(this == cur.text());
1532 if (cur.selection()) {
1533 from = cur.selBegin();
1537 getWord(from, to, lyx::PARTIAL_WORD);
1538 setCursor(cur, to.par(), to.pos() + 1);
1541 recordUndoSelection(cur);
1543 pos_type pos = from.pos();
1544 int par = from.par();
1546 while (par != int(pars_.size()) && (pos != to.pos() || par != to.par())) {
1548 if (pos == pars_[pit].size()) {
1553 unsigned char c = pars_[pit].getChar(pos);
1554 if (c != Paragraph::META_INSET) {
1556 case text_lowercase:
1559 case text_capitalization:
1561 action = text_lowercase;
1563 case text_uppercase:
1568 #ifdef WITH_WARNINGS
1571 pars_[pit].setChar(pos, c);
1577 void LyXText::Delete(LCursor & cur)
1579 BOOST_ASSERT(this == cur.text());
1580 if (cur.pos() != cur.lastpos()) {
1581 recordUndo(cur, Undo::DELETE, cur.par());
1582 setCursorIntern(cur, cur.par(), cur.pos() + 1, false, cur.boundary());
1585 // should we do anything in an else branch?
1589 void LyXText::backspace(LCursor & cur)
1591 BOOST_ASSERT(this == cur.text());
1592 if (cur.pos() == 0) {
1593 // The cursor is at the beginning of a paragraph, so
1594 // the the backspace will collapse two paragraphs into
1597 // but it's not allowed unless it's new
1598 Paragraph & par = cur.paragraph();
1599 if (par.isChangeEdited(0, par.size()))
1602 // we may paste some paragraphs
1604 // is it an empty paragraph?
1605 pos_type lastpos = cur.lastpos();
1606 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1607 // This is an empty paragraph and we delete it just
1608 // by moving the cursor one step
1609 // left and let the DeleteEmptyParagraphMechanism
1610 // handle the actual deletion of the paragraph.
1612 if (cur.par() != 0) {
1614 // the layout things can change the height of a row !
1621 recordUndo(cur, Undo::DELETE, cur.par() - 1);
1623 par_type tmppit = cur.par();
1624 // We used to do cursorLeftIntern() here, but it is
1625 // not a good idea since it triggers the auto-delete
1626 // mechanism. So we do a cursorLeftIntern()-lite,
1627 // without the dreaded mechanism. (JMarc)
1628 if (cur.par() != 0) {
1629 // steps into the above paragraph.
1630 setCursorIntern(cur, cur.par() - 1,
1631 pars_[cur.par() - 1].size(),
1635 // Pasting is not allowed, if the paragraphs have different
1636 // layout. I think it is a real bug of all other
1637 // word processors to allow it. It confuses the user.
1638 // Correction: Pasting is always allowed with standard-layout
1639 Buffer & buf = cur.buffer();
1640 BufferParams const & bufparams = buf.params();
1641 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1642 par_type const cpit = cur.par();
1645 && (pars_[cpit].layout() == pars_[tmppit].layout()
1646 || pars_[tmppit].layout() == tclass.defaultLayout())
1647 && pars_[cpit].getAlign() == pars_[tmppit].getAlign()) {
1648 mergeParagraph(bufparams, buf.paragraphs(), cpit);
1650 if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
1653 // the counters may have changed
1655 setCursor(cur, cur.par(), cur.pos(), false);
1658 // this is the code for a normal backspace, not pasting
1660 recordUndo(cur, Undo::DELETE);
1661 // We used to do cursorLeftIntern() here, but it is
1662 // not a good idea since it triggers the auto-delete
1663 // mechanism. So we do a cursorLeftIntern()-lite,
1664 // without the dreaded mechanism. (JMarc)
1665 setCursorIntern(cur, cur.par(), cur.pos() - 1,
1666 false, cur.boundary());
1667 cur.paragraph().erase(cur.pos());
1670 if (cur.pos() == cur.lastpos())
1671 setCurrentFont(cur);
1674 setCursor(cur, cur.par(), cur.pos(), false, cur.boundary());
1678 Paragraph & LyXText::getPar(par_type par) const
1680 //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
1681 BOOST_ASSERT(par >= 0);
1682 BOOST_ASSERT(par < int(paragraphs().size()));
1683 return paragraphs()[par];
1687 Row const & LyXText::firstRow() const
1689 return *paragraphs().front().rows.begin();
1693 void LyXText::redoParagraphInternal(par_type const pit)
1695 // remove rows of paragraph, keep track of height changes
1696 Paragraph & par = pars_[pit];
1697 height_ -= par.height;
1705 InsetList::iterator ii = par.insetlist.begin();
1706 InsetList::iterator iend = par.insetlist.end();
1707 for (; ii != iend; ++ii) {
1709 int const w = maxwidth_ - leftMargin(pit) - rightMargin(par);
1710 MetricsInfo mi(bv(), getFont(par, ii->pos), w);
1711 ii->inset->metrics(mi, dim);
1714 // rebreak the paragraph
1715 par.setBeginOfBody();
1719 rowBreakPoint(pit, row);
1720 setRowWidth(pit, row);
1721 setHeightOfRow(pit, row);
1722 row.y_offset(par.height);
1723 par.rows.push_back(row);
1724 par.width = std::max(par.width, row.width());
1725 par.height += row.height();
1727 } while (z < par.size());
1729 height_ += par.height;
1730 //lyxerr << "redoParagraph: " << par.rows.size() << " rows\n";
1734 void LyXText::redoParagraphs(par_type pit, par_type end)
1736 for (; pit != end; ++pit)
1737 redoParagraphInternal(pit);
1738 updateParPositions();
1743 void LyXText::redoParagraph(par_type pit)
1745 redoParagraphInternal(pit);
1746 updateParPositions();
1750 void LyXText::fullRebreak()
1752 redoParagraphs(0, paragraphs().size());
1753 bv()->cursor().resetAnchor();
1757 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1759 //BOOST_ASSERT(mi.base.textwidth);
1760 if (mi.base.textwidth)
1761 maxwidth_ = mi.base.textwidth;
1762 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1763 //<< " maxWidth: " << maxwidth << "\nfont: " << mi.base.font
1766 // Rebuild row cache. This recomputes height as well.
1767 redoParagraphs(0, paragraphs().size());
1769 width_ = maxParagraphWidth(paragraphs());
1772 dim.asc = firstRow().ascent_of_text();
1773 dim.des = height_ - dim.asc;
1778 // only used for inset right now. should also be used for main text
1779 void LyXText::draw(PainterInfo & pi, int x, int y) const
1783 paintTextInset(*this, pi);
1787 // only used for inset right now. should also be used for main text
1788 void LyXText::drawSelection(PainterInfo &, int, int) const
1790 //lyxerr << "LyXText::drawSelection at " << x << " " << y << endl;
1794 bool LyXText::isLastRow(par_type pit, Row const & row) const
1796 return row.endpos() >= pars_[pit].size()
1797 && pit + 1 == par_type(paragraphs().size());
1801 bool LyXText::isFirstRow(par_type pit, Row const & row) const
1803 return row.pos() == 0 && pit == 0;
1807 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1808 word_location const loc)
1810 Paragraph const & from_par = pars_[from.par()];
1812 case lyx::WHOLE_WORD_STRICT:
1813 if (from.pos() == 0 || from.pos() == from_par.size()
1814 || !from_par.isWord(from.pos())
1815 || !from_par.isWord(from.pos() - 1)) {
1819 // no break here, we go to the next
1821 case lyx::WHOLE_WORD:
1822 // If we are already at the beginning of a word, do nothing
1823 if (!from.pos() || !from_par.isWord(from.pos() - 1))
1825 // no break here, we go to the next
1827 case lyx::PREVIOUS_WORD:
1828 // always move the cursor to the beginning of previous word
1829 while (from.pos() && from_par.isWord(from.pos() - 1))
1832 case lyx::NEXT_WORD:
1833 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1836 case lyx::PARTIAL_WORD:
1837 // no need to move the 'from' cursor
1841 Paragraph & to_par = pars_[to.par()];
1842 while (to.pos() < to_par.size() && to_par.isWord(to.pos()))
1847 void LyXText::write(Buffer const & buf, std::ostream & os) const
1849 ParagraphList::const_iterator pit = paragraphs().begin();
1850 ParagraphList::const_iterator end = paragraphs().end();
1851 Paragraph::depth_type dth = 0;
1852 for (; pit != end; ++pit)
1853 pit->write(buf, os, buf.params(), dth);
1857 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1859 static Change current_change;
1861 Paragraph::depth_type depth = 0;
1863 while (lex.isOK()) {
1865 string const token = lex.getString();
1870 if (token == "\\end_inset") {
1874 if (token == "\\end_document") {
1881 if (token == "\\begin_layout") {
1882 lex.pushToken(token);
1885 par.params().depth(depth);
1886 if (buf.params().tracking_changes)
1888 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1889 pars_.push_back(par);
1891 // FIXME: goddamn InsetTabular makes us pass a Buffer
1893 ::readParagraph(buf, pars_.back(), lex);
1895 } else if (token == "\\begin_deeper") {
1897 } else if (token == "\\end_deeper") {
1899 lex.printError("\\end_deeper: " "depth is already null");
1912 int LyXText::ascent() const
1914 return firstRow().ascent_of_text();
1918 int LyXText::descent() const
1920 return height_ - firstRow().ascent_of_text();
1924 int LyXText::cursorX(CursorSlice const & cur) const
1926 par_type const pit = cur.par();
1927 Paragraph const & par = pars_[pit];
1928 if (par.rows.empty())
1931 Row const & row = *par.getRow(cur.pos());
1933 pos_type pos = cur.pos();
1934 pos_type cursor_vpos = 0;
1936 RowMetrics const m = computeRowMetrics(pit, row);
1939 pos_type const row_pos = row.pos();
1940 pos_type const end = row.endpos();
1943 cursor_vpos = row_pos;
1944 else if (pos >= end)
1945 cursor_vpos = isRTL(par) ? row_pos : end;
1946 else if (pos > row_pos && pos >= end)
1947 // Place cursor after char at (logical) position pos - 1
1948 cursor_vpos = (bidi.level(pos - 1) % 2 == 0)
1949 ? bidi.log2vis(pos - 1) + 1 : bidi.log2vis(pos - 1);
1951 // Place cursor before char at (logical) position pos
1952 cursor_vpos = (bidi.level(pos) % 2 == 0)
1953 ? bidi.log2vis(pos) : bidi.log2vis(pos) + 1;
1955 pos_type body_pos = par.beginOfBody();
1957 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1960 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1961 pos_type pos = bidi.vis2log(vpos);
1962 if (body_pos > 0 && pos == body_pos - 1) {
1964 + font_metrics::width(par.layout()->labelsep,
1966 if (par.isLineSeparator(body_pos - 1))
1967 x -= singleWidth(par, body_pos - 1);
1970 if (hfillExpansion(par, row, pos)) {
1971 x += singleWidth(par, pos);
1972 if (pos >= body_pos)
1976 } else if (par.isSeparator(pos)) {
1977 x += singleWidth(par, pos);
1978 if (pos >= body_pos)
1981 x += singleWidth(par, pos);
1983 return xo_ + int(x);
1987 int LyXText::cursorY(CursorSlice const & cur) const
1989 Paragraph const & par = getPar(cur.par());
1990 Row const & row = *par.getRow(cur.pos());
1991 return yo_ + par.y + row.y_offset() + row.baseline();
1995 // Returns the current font and depth as a message.
1996 string LyXText::currentState(LCursor & cur)
1998 BOOST_ASSERT(this == cur.text());
1999 Buffer & buf = cur.buffer();
2000 Paragraph const & par = cur.paragraph();
2001 std::ostringstream os;
2003 bool const show_change = buf.params().tracking_changes
2004 && cur.pos() != cur.lastpos()
2005 && par.lookupChange(cur.pos()) != Change::UNCHANGED;
2008 Change change = par.lookupChangeFull(cur.pos());
2009 Author const & a = buf.params().authors().get(change.author);
2010 os << _("Change: ") << a.name();
2011 if (!a.email().empty())
2012 os << " (" << a.email() << ")";
2013 if (change.changetime)
2014 os << _(" at ") << ctime(&change.changetime);
2018 // I think we should only show changes from the default
2020 LyXFont font = real_current_font;
2021 font.reduce(buf.params().getLyXTextClass().defaultfont());
2023 // avoid _(...) re-entrance problem
2024 string const s = font.stateText(&buf.params());
2025 os << bformat(_("Font: %1$s"), s);
2027 // os << bformat(_("Font: %1$s"), font.stateText(&buf.params));
2029 // The paragraph depth
2030 int depth = cur.paragraph().getDepth();
2032 os << bformat(_(", Depth: %1$s"), tostr(depth));
2034 // The paragraph spacing, but only if different from
2036 Spacing const & spacing = par.params().spacing();
2037 if (!spacing.isDefault()) {
2038 os << _(", Spacing: ");
2039 switch (spacing.getSpace()) {
2040 case Spacing::Single:
2043 case Spacing::Onehalf:
2046 case Spacing::Double:
2049 case Spacing::Other:
2050 os << _("Other (") << spacing.getValue() << ')';
2052 case Spacing::Default:
2053 // should never happen, do nothing
2058 #ifdef DEVEL_VERSION
2059 os << _(", Inset: ") << &cur.inset();
2060 os << _(", Paragraph: ") << cur.par();
2061 os << _(", Id: ") << par.id();
2062 os << _(", Position: ") << cur.pos();
2063 Row & row = cur.textRow();
2064 os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2070 string LyXText::getPossibleLabel(LCursor & cur) const
2072 par_type pit = cur.par();
2074 LyXLayout_ptr layout = pars_[pit].layout();
2076 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2077 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
2078 if (layout2->latextype != LATEX_PARAGRAPH) {
2084 string text = layout->latexname().substr(0, 3);
2085 if (layout->latexname() == "theorem")
2086 text = "thm"; // Create a correct prefix for prettyref
2089 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2092 string par_text = pars_[pit].asString(cur.buffer(), false);
2093 for (int i = 0; i < lyxrc.label_init_length; ++i) {
2094 if (par_text.empty())
2097 par_text = split(par_text, head, ' ');
2098 // Is it legal to use spaces in labels ?
2108 // Manhattan distance to nearest corner
2109 int LyXText::dist(int x, int y) const
2116 else if (x > xo_ + int(width_))
2117 xx = x - xo_ - width_;
2119 if (y < yo_ - ascent())
2120 yy = yo_ - ascent() - y;
2121 else if (y > yo_ + descent())
2122 yy = y - yo_ - descent();