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 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
163 string const & token, LyXFont & font)
165 static Change change;
167 BufferParams const & bp = buf.params();
169 if (token[0] != '\\') {
170 string::const_iterator cit = token.begin();
171 for (; cit != token.end(); ++cit) {
172 par.insertChar(par.size(), (*cit), font, change);
174 } else if (token == "\\begin_layout") {
176 string layoutname = lex.getString();
178 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
181 LyXTextClass const & tclass = bp.getLyXTextClass();
183 if (layoutname.empty()) {
184 layoutname = tclass.defaultLayoutName();
187 bool hasLayout = tclass.hasLayout(layoutname);
190 buf.error(ErrorItem(_("Unknown layout"),
191 bformat(_("Layout '%1$s' does not exists in textclass '%2$s'\nTrying to use the default instead.\n"),
192 layoutname, tclass.name()), par.id(), 0, par.size()));
193 layoutname = tclass.defaultLayoutName();
196 par.layout(bp.getLyXTextClass()[layoutname]);
198 // Test whether the layout is obsolete.
199 LyXLayout_ptr const & layout = par.layout();
200 if (!layout->obsoleted_by().empty())
201 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
203 par.params().read(lex);
205 } else if (token == "\\end_layout") {
206 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
207 << "Missing \\begin_layout?.\n";
208 } else if (token == "\\end_inset") {
209 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
210 << "Missing \\begin_inset?.\n";
211 } else if (token == "\\begin_inset") {
212 InsetBase * inset = readInset(lex, buf);
214 par.insertInset(par.size(), inset, font, change);
217 string line = lex.getString();
218 buf.error(ErrorItem(_("Unknown Inset"), line,
219 par.id(), 0, par.size()));
221 } else if (token == "\\family") {
223 font.setLyXFamily(lex.getString());
224 } else if (token == "\\series") {
226 font.setLyXSeries(lex.getString());
227 } else if (token == "\\shape") {
229 font.setLyXShape(lex.getString());
230 } else if (token == "\\size") {
232 font.setLyXSize(lex.getString());
233 } else if (token == "\\lang") {
235 string const tok = lex.getString();
236 Language const * lang = languages.getLanguage(tok);
238 font.setLanguage(lang);
240 font.setLanguage(bp.language);
241 lex.printError("Unknown language `$$Token'");
243 } else if (token == "\\numeric") {
245 font.setNumber(font.setLyXMisc(lex.getString()));
246 } else if (token == "\\emph") {
248 font.setEmph(font.setLyXMisc(lex.getString()));
249 } else if (token == "\\bar") {
251 string const tok = lex.getString();
254 font.setUnderbar(LyXFont::ON);
255 else if (tok == "no")
256 font.setUnderbar(LyXFont::OFF);
257 else if (tok == "default")
258 font.setUnderbar(LyXFont::INHERIT);
260 lex.printError("Unknown bar font flag "
262 } else if (token == "\\noun") {
264 font.setNoun(font.setLyXMisc(lex.getString()));
265 } else if (token == "\\color") {
267 font.setLyXColor(lex.getString());
268 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
270 // Insets don't make sense in a free-spacing context! ---Kayvan
271 if (par.isFreeSpacing()) {
272 if (token == "\\InsetSpace")
273 par.insertChar(par.size(), ' ', font, change);
274 else if (lex.isOK()) {
276 string const next_token = lex.getString();
277 if (next_token == "\\-")
278 par.insertChar(par.size(), '-', font, change);
280 lex.printError("Token `$$Token' "
282 "paragraph layout!");
286 auto_ptr<InsetBase> inset;
287 if (token == "\\SpecialChar" )
288 inset.reset(new InsetSpecialChar);
290 inset.reset(new InsetSpace);
291 inset->read(buf, lex);
292 par.insertInset(par.size(), inset.release(),
295 } else if (token == "\\i") {
296 auto_ptr<InsetBase> inset(new InsetLatexAccent);
297 inset->read(buf, lex);
298 par.insertInset(par.size(), inset.release(), font, change);
299 } else if (token == "\\backslash") {
300 par.insertChar(par.size(), '\\', font, change);
301 } else if (token == "\\newline") {
302 auto_ptr<InsetBase> inset(new InsetNewline);
303 inset->read(buf, lex);
304 par.insertInset(par.size(), inset.release(), font, change);
305 } else if (token == "\\LyXTable") {
306 auto_ptr<InsetBase> inset(new InsetTabular(buf));
307 inset->read(buf, lex);
308 par.insertInset(par.size(), inset.release(), font, change);
309 } else if (token == "\\bibitem") {
310 InsetCommandParams p("bibitem", "dummy");
311 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
312 inset->read(buf, lex);
313 par.insertInset(par.size(), inset.release(), font, change);
314 } else if (token == "\\hfill") {
315 par.insertInset(par.size(), new InsetHFill, font, change);
316 } else if (token == "\\lyxline") {
317 par.insertInset(par.size(), new InsetLine, font, change);
318 } else if (token == "\\newpage") {
319 par.insertInset(par.size(), new InsetPagebreak, font, change);
320 } else if (token == "\\change_unchanged") {
321 // Hack ! Needed for empty paragraphs :/
322 // FIXME: is it still ??
325 change = Change(Change::UNCHANGED);
326 } else if (token == "\\change_inserted") {
328 std::istringstream is(lex.getString());
332 change = Change(Change::INSERTED, bp.author_map[aid], ct);
333 } else if (token == "\\change_deleted") {
335 std::istringstream is(lex.getString());
339 change = Change(Change::DELETED, bp.author_map[aid], ct);
342 buf.error(ErrorItem(_("Unknown token"),
343 bformat(_("Unknown token: %1$s %2$s\n"), token, lex.getString()),
344 par.id(), 0, par.size()));
349 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex)
352 string token = lex.getString();
357 readParToken(buf, par, lex, token, font);
360 token = lex.getString();
365 if (token == "\\end_layout") {
366 //Ok, paragraph finished
370 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
371 << token << '\'' << endl;
372 if (token == "\\begin_layout" || token == "\\end_document"
373 || token == "\\end_inset" || token == "\\begin_deeper"
374 || token == "\\end_deeper") {
375 lex.pushToken(token);
376 lyxerr << "Paragraph ended in line "
377 << lex.getLineNo() << "\n"
378 << "Missing \\end_layout.\n";
388 BufferView * LyXText::bv()
390 BOOST_ASSERT(bv_owner != 0);
395 BufferView * LyXText::bv() const
397 BOOST_ASSERT(bv_owner != 0);
402 double LyXText::spacing(Paragraph const & par) const
404 if (par.params().spacing().isDefault())
405 return bv()->buffer()->params().spacing().getValue();
406 return par.params().spacing().getValue();
410 void LyXText::updateParPositions()
413 par_type end = pars_.size();
414 for (height_ = 0; pit != end; ++pit) {
415 pars_[pit].y = height_;
416 height_ += pars_[pit].height;
421 int LyXText::width() const
427 int LyXText::height() const
433 int LyXText::singleWidth(Paragraph const & par, pos_type pos) const
435 return singleWidth(par, pos, par.getChar(pos), getFont(par, pos));
439 int LyXText::singleWidth(Paragraph const & par,
440 pos_type pos, char c, LyXFont const & font) const
442 BOOST_ASSERT(pos < par.size());
444 // The most common case is handled first (Asger)
445 if (IsPrintable(c)) {
446 if (font.language()->RightToLeft()) {
447 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
448 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
449 && font.language()->lang() == "arabic") {
450 if (Encodings::IsComposeChar_arabic(c))
453 c = par.transformChar(c, pos);
454 } else if (font.language()->lang() == "hebrew" &&
455 Encodings::IsComposeChar_hebrew(c))
458 return font_metrics::width(c, font);
461 if (c == Paragraph::META_INSET)
462 return par.getInset(pos)->width();
464 if (IsSeparatorChar(c))
466 return font_metrics::width(c, font);
470 int LyXText::leftMargin(par_type pit) const
472 return leftMargin(pit, pars_[pit].size());
476 int LyXText::leftMargin(par_type const pit, pos_type const pos) const
478 Paragraph const & par = pars_[pit];
479 LyXTextClass const & tclass =
480 bv()->buffer()->params().getLyXTextClass();
481 LyXLayout_ptr const & layout = par.layout();
483 string parindent = layout->parindent;
488 l_margin += changebarMargin();
490 l_margin += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
492 if (par.getDepth() != 0) {
493 // find the next level paragraph
494 par_type newpar = outerHook(pit, pars_);
495 if (newpar != par_type(pars_.size())) {
496 if (pars_[newpar].layout()->isEnvironment()) {
497 l_margin = leftMargin(newpar);
499 if (par.layout() == tclass.defaultLayout()) {
500 if (pars_[newpar].params().noindent())
503 parindent = pars_[newpar].layout()->parindent;
508 LyXFont const labelfont = getLabelFont(par);
509 switch (layout->margintype) {
511 if (!layout->leftmargin.empty())
512 l_margin += font_metrics::signedWidth(layout->leftmargin,
513 tclass.defaultfont());
514 if (!par.getLabelstring().empty()) {
515 l_margin += font_metrics::signedWidth(layout->labelindent,
517 l_margin += font_metrics::width(par.getLabelstring(),
519 l_margin += font_metrics::width(layout->labelsep, labelfont);
524 l_margin += font_metrics::signedWidth(layout->labelindent, labelfont);
525 // The width of an empty par, even with manual label, should be 0
526 if (!par.empty() && pos >= par.beginOfBody()) {
527 if (!par.getLabelWidthString().empty()) {
528 l_margin += font_metrics::width(par.getLabelWidthString(),
530 l_margin += font_metrics::width(layout->labelsep, labelfont);
536 l_margin += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
537 / (par.getDepth() + 4);
540 case MARGIN_FIRST_DYNAMIC:
541 if (layout->labeltype == LABEL_MANUAL) {
542 if (pos >= par.beginOfBody()) {
543 l_margin += font_metrics::signedWidth(layout->leftmargin,
546 l_margin += font_metrics::signedWidth(layout->labelindent,
550 // Special case to fix problems with
552 || (layout->labeltype == LABEL_STATIC
553 && layout->latextype == LATEX_ENVIRONMENT
554 && !isFirstInSequence(pit, pars_))) {
555 l_margin += font_metrics::signedWidth(layout->leftmargin,
557 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
558 && layout->labeltype != LABEL_BIBLIO
559 && layout->labeltype !=
560 LABEL_CENTERED_TOP_ENVIRONMENT) {
561 l_margin += font_metrics::signedWidth(layout->labelindent,
563 l_margin += font_metrics::width(layout->labelsep, labelfont);
564 l_margin += font_metrics::width(par.getLabelstring(),
569 case MARGIN_RIGHT_ADDRESS_BOX: {
571 // ok, a terrible hack. The left margin depends on the widest
572 // row in this paragraph.
573 RowList::iterator rit = par.rows.begin();
574 RowList::iterator end = par.rows.end();
576 #warning This is wrong.
578 int minfill = maxwidth_;
579 for ( ; rit != end; ++rit)
580 if (rit->fill() < minfill)
581 minfill = rit->fill();
582 l_margin += font_metrics::signedWidth(layout->leftmargin,
583 tclass.defaultfont());
586 // also wrong, but much shorter.
587 l_margin += maxwidth_ / 2;
592 if (!par.params().leftIndent().zero())
593 l_margin += par.params().leftIndent().inPixels(maxwidth_);
597 if (par.params().align() == LYX_ALIGN_LAYOUT)
598 align = layout->align;
600 align = par.params().align();
602 // set the correct parindent
604 && (layout->labeltype == LABEL_NO_LABEL
605 || layout->labeltype == LABEL_TOP_ENVIRONMENT
606 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
607 || (layout->labeltype == LABEL_STATIC
608 && layout->latextype == LATEX_ENVIRONMENT
609 && !isFirstInSequence(pit, pars_)))
610 && align == LYX_ALIGN_BLOCK
611 && !par.params().noindent()
612 // in tabulars and ert paragraphs are never indented!
613 && (par.ownerCode() != InsetBase::TEXT_CODE
614 && par.ownerCode() != InsetBase::ERT_CODE)
615 && (par.layout() != tclass.defaultLayout()
616 || bv()->buffer()->params().paragraph_separation ==
617 BufferParams::PARSEP_INDENT))
619 l_margin += font_metrics::signedWidth(parindent, tclass.defaultfont());
626 int LyXText::rightMargin(Paragraph const & par) const
628 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
630 // We do not want rightmargins on inner texts.
631 if (bv()->text() != this)
636 + font_metrics::signedWidth(tclass.rightmargin(),
637 tclass.defaultfont())
638 + font_metrics::signedWidth(par.layout()->rightmargin,
639 tclass.defaultfont())
640 * 4 / (par.getDepth() + 4);
647 int LyXText::labelEnd(par_type const pit) const
649 // labelEnd is only needed if the layout fills a flushleft label.
650 if (pars_[pit].layout()->margintype != MARGIN_MANUAL)
652 // return the beginning of the body
653 return leftMargin(pit);
659 // this needs special handling - only newlines count as a break point
660 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
662 pos_type const end = par.size();
665 if (par.isNewline(i))
674 void LyXText::rowBreakPoint(par_type const pit, Row & row) const
676 Paragraph const & par = pars_[pit];
677 pos_type const end = par.size();
678 pos_type const pos = row.pos();
684 // maximum pixel width of a row
685 int width = maxwidth_ - rightMargin(par); // - leftMargin(pit, row);
691 LyXLayout_ptr const & layout = par.layout();
693 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
694 row.endpos(addressBreakPoint(pos, par));
698 pos_type const body_pos = par.beginOfBody();
701 // Now we iterate through until we reach the right margin
702 // or the end of the par, then choose the possible break
705 int const left = leftMargin(pit, pos);
708 // pixel width since last breakpoint
711 FontIterator fi = FontIterator(*this, pit, pos);
712 pos_type point = end;
714 for ( ; i < end; ++i, ++fi) {
715 char const c = par.getChar(i);
718 int thiswidth = singleWidth(par, i, c, *fi);
720 // add the auto-hfill from label end to the body
721 if (body_pos && i == body_pos) {
722 int add = font_metrics::width(layout->labelsep, getLabelFont(par));
723 if (par.isLineSeparator(i - 1))
724 add -= singleWidth(par, i - 1);
726 add = std::max(add, labelEnd(pit) - x);
731 chunkwidth += thiswidth;
734 // break before a character that will fall off
735 // the right of the row
737 // if no break before, break here
738 if (point == end || chunkwidth >= width - left) {
745 // exit on last registered breakpoint:
749 if (par.isNewline(i)) {
755 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
760 if (par.isInset(i) && par.getInset(i)->display()) {
766 if (!par.isInset(i) || par.getInset(i)->isChar()) {
767 // some insets are line separators too
768 if (par.isLineSeparator(i)) {
769 // register breakpoint:
776 // maybe found one, but the par is short enough.
777 if (i == end && x < width)
780 // manual labels cannot be broken in LaTeX. But we
781 // want to make our on-screen rendering of footnotes
783 if (body_pos && point < body_pos)
790 void LyXText::setRowWidth(par_type const pit, Row & row) const
792 // get the pure distance
793 pos_type const end = row.endpos();
795 Paragraph const & par = pars_[pit];
796 string labelsep = par.layout()->labelsep;
797 int w = leftMargin(pit, row.pos());
799 pos_type const body_pos = par.beginOfBody();
800 pos_type i = row.pos();
803 FontIterator fi = FontIterator(*this, pit, i);
804 for ( ; i < end; ++i, ++fi) {
805 if (body_pos > 0 && i == body_pos) {
806 w += font_metrics::width(labelsep, getLabelFont(par));
807 if (par.isLineSeparator(i - 1))
808 w -= singleWidth(par, i - 1);
809 w = max(w, labelEnd(pit));
811 char const c = par.getChar(i);
812 w += singleWidth(par, i, c, *fi);
816 if (body_pos > 0 && body_pos >= end) {
817 w += font_metrics::width(labelsep, getLabelFont(par));
818 if (end > 0 && par.isLineSeparator(end - 1))
819 w -= singleWidth(par, end - 1);
820 w = max(w, labelEnd(pit));
823 row.width(w + rightMargin(par));
827 // returns the minimum space a manual label needs on the screen in pixel
828 int LyXText::labelFill(Paragraph const & par, Row const & row) const
830 pos_type last = par.beginOfBody();
832 BOOST_ASSERT(last > 0);
834 // -1 because a label ends with a space that is in the label
837 // a separator at this end does not count
838 if (par.isLineSeparator(last))
842 for (pos_type i = row.pos(); i <= last; ++i)
843 w += singleWidth(par, i);
845 string const & label = par.params().labelWidthString();
849 return max(0, font_metrics::width(label, getLabelFont(par)) - w);
853 LColor_color LyXText::backgroundColor() const
855 return LColor_color(LColor::color(background_color_));
859 void LyXText::setHeightOfRow(par_type const pit, Row & row)
861 Paragraph const & par = pars_[pit];
862 // get the maximum ascent and the maximum descent
863 double layoutasc = 0;
864 double layoutdesc = 0;
865 double const dh = defaultRowHeight();
867 // ok, let us initialize the maxasc and maxdesc value.
868 // Only the fontsize count. The other properties
869 // are taken from the layoutfont. Nicer on the screen :)
870 LyXLayout_ptr const & layout = par.layout();
872 // as max get the first character of this row then it can
873 // increase but not decrease the height. Just some point to
874 // start with so we don't have to do the assignment below too
876 LyXFont font = getFont(par, row.pos());
877 LyXFont::FONT_SIZE const tmpsize = font.size();
878 font = getLayoutFont(pit);
879 LyXFont::FONT_SIZE const size = font.size();
880 font.setSize(tmpsize);
882 LyXFont labelfont = getLabelFont(par);
884 // these are minimum values
885 double const spacing_val = layout->spacing.getValue() * spacing(par);
886 //lyxerr << "spacing_val = " << spacing_val << endl;
887 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
888 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
890 // insets may be taller
891 InsetList::const_iterator ii = par.insetlist.begin();
892 InsetList::const_iterator iend = par.insetlist.end();
893 for ( ; ii != iend; ++ii) {
894 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
895 maxasc = max(maxasc, ii->inset->ascent());
896 maxdesc = max(maxdesc, ii->inset->descent());
900 // Check if any custom fonts are larger (Asger)
901 // This is not completely correct, but we can live with the small,
902 // cosmetic error for now.
904 pos_type const pos_end = row.endpos();
906 LyXFont::FONT_SIZE maxsize =
907 par.highestFontInRange(row.pos(), pos_end, size);
908 if (maxsize > font.size()) {
909 font.setSize(maxsize);
910 maxasc = max(maxasc, font_metrics::maxAscent(font));
911 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
914 // This is nicer with box insets:
918 row.ascent_of_text(maxasc);
921 if (row.pos() == 0) {
922 BufferParams const & bufparams = bv()->buffer()->params();
923 // some parksips VERY EASY IMPLEMENTATION
924 if (bv()->buffer()->params().paragraph_separation
925 == BufferParams::PARSEP_SKIP
927 && ((layout->isParagraph() && par.getDepth() == 0)
928 || (pars_[pit - 1].layout()->isParagraph()
929 && pars_[pit - 1].getDepth() == 0)))
931 maxasc += bufparams.getDefSkip().inPixels(*bv());
934 if (pars_[pit].params().startOfAppendix())
935 maxasc += int(3 * dh);
937 // This is special code for the chapter, since the label of this
938 // layout is printed in an extra row
939 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
940 labeladdon = int(font_metrics::maxHeight(labelfont)
941 * layout->spacing.getValue()
945 // special code for the top label
946 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
947 || layout->labeltype == LABEL_BIBLIO
948 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
949 && isFirstInSequence(pit, paragraphs())
950 && !par.getLabelstring().empty())
953 font_metrics::maxHeight(labelfont)
954 * layout->spacing.getValue()
956 + (layout->topsep + layout->labelbottomsep) * dh);
959 // Add the layout spaces, for example before and after
960 // a section, or between the items of a itemize or enumerate
963 par_type prev = depthHook(pit, pars_, par.getDepth());
965 && pars_[prev].layout() == layout
966 && pars_[prev].getDepth() == par.getDepth()
967 && pars_[prev].getLabelWidthString() == par.getLabelWidthString())
969 layoutasc = layout->itemsep * dh;
970 } else if (pit != 0 || row.pos() != 0) {
971 if (layout->topsep > 0)
972 layoutasc = layout->topsep * dh;
975 prev = outerHook(pit, pars_);
976 if (prev != par_type(pars_.size())) {
977 maxasc += int(pars_[prev].layout()->parsep * dh);
978 } else if (pit != 0) {
979 if (pars_[pit - 1].getDepth() != 0 ||
980 pars_[pit - 1].layout() == layout) {
981 maxasc += int(layout->parsep * dh);
986 // is it a bottom line?
987 if (row.endpos() >= par.size()) {
988 // add the layout spaces, for example before and after
989 // a section, or between the items of a itemize or enumerate
991 par_type nextpit = pit + 1;
992 if (nextpit != par_type(pars_.size())) {
997 if (pars_[cpit].getDepth() > pars_[nextpit].getDepth()) {
998 usual = pars_[cpit].layout()->bottomsep * dh;
999 cpit = depthHook(cpit, paragraphs(), pars_[nextpit].getDepth());
1000 if (pars_[cpit].layout() != pars_[nextpit].layout()
1001 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1003 unusual = pars_[cpit].layout()->bottomsep * dh;
1005 layoutdesc = max(unusual, usual);
1006 } else if (pars_[cpit].getDepth() == pars_[nextpit].getDepth()) {
1007 if (pars_[cpit].layout() != pars_[nextpit].layout()
1008 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1009 layoutdesc = int(pars_[cpit].layout()->bottomsep * dh);
1014 // incalculate the layout spaces
1015 maxasc += int(layoutasc * 2 / (2 + pars_[pit].getDepth()));
1016 maxdesc += int(layoutdesc * 2 / (2 + pars_[pit].getDepth()));
1018 row.height(maxasc + maxdesc + labeladdon);
1019 row.baseline(maxasc + labeladdon);
1020 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
1028 void LyXText::breakParagraph(LCursor & cur, char keep_layout)
1030 BOOST_ASSERT(this == cur.text());
1031 // allow only if at start or end, or all previous is new text
1032 Paragraph & cpar = cur.paragraph();
1033 par_type cpit = cur.par();
1035 if (cur.pos() != 0 && cur.pos() != cur.lastpos()
1036 && cpar.isChangeEdited(0, cur.pos()))
1039 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
1040 LyXLayout_ptr const & layout = cpar.layout();
1042 // this is only allowed, if the current paragraph is not empty
1043 // or caption and if it has not the keepempty flag active
1044 if (cur.lastpos() == 0 && !cpar.allowEmpty()
1045 && layout->labeltype != LABEL_SENSITIVE)
1048 // a layout change may affect also the following paragraph
1049 recUndo(cur.par(), undoSpan(cur.par()) - 1);
1051 // Always break behind a space
1052 // It is better to erase the space (Dekel)
1053 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
1054 cpar.erase(cur.pos());
1056 // break the paragraph
1060 keep_layout = layout->isEnvironment();
1062 // we need to set this before we insert the paragraph. IMO the
1063 // breakParagraph call should return a bool if it inserts the
1064 // paragraph before or behind and we should react on that one
1065 // but we can fix this in 1.3.0 (Jug 20020509)
1066 bool const isempty = cpar.allowEmpty() && cpar.empty();
1067 ::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
1068 cur.pos(), keep_layout);
1071 par_type next_par = cpit + 1;
1073 // well this is the caption hack since one caption is really enough
1074 if (layout->labeltype == LABEL_SENSITIVE) {
1076 // set to standard-layout
1077 pars_[cpit].applyLayout(tclass.defaultLayout());
1079 // set to standard-layout
1080 pars_[next_par].applyLayout(tclass.defaultLayout());
1083 // if the cursor is at the beginning of a row without prior newline,
1085 // This touches only the screen-update. Otherwise we would may have
1086 // an empty row on the screen
1087 if (cur.pos() != 0 && cur.textRow().pos() == cur.pos()
1088 && !pars_[cpit].isNewline(cur.pos() - 1))
1093 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
1094 pars_[next_par].erase(0);
1097 redoParagraph(cpit);
1098 redoParagraph(next_par);
1100 // This check is necessary. Otherwise the new empty paragraph will
1101 // be deleted automatically. And it is more friendly for the user!
1102 if (cur.pos() != 0 || isempty)
1103 setCursor(cur, cur.par() + 1, 0);
1105 setCursor(cur, cur.par(), 0);
1109 // convenience function
1110 void LyXText::redoParagraph(LCursor & cur)
1112 BOOST_ASSERT(this == cur.text());
1113 cur.clearSelection();
1114 redoParagraph(cur.par());
1115 setCursorIntern(cur, cur.par(), cur.pos());
1119 // insert a character, moves all the following breaks in the
1120 // same Paragraph one to the right and make a rebreak
1121 void LyXText::insertChar(LCursor & cur, char c)
1123 BOOST_ASSERT(this == cur.text());
1124 BOOST_ASSERT(c != Paragraph::META_INSET);
1126 recordUndo(cur, Undo::INSERT);
1128 Paragraph & par = cur.paragraph();
1129 // try to remove this
1130 par_type const pit = cur.par();
1132 bool const freeSpacing = par.layout()->free_spacing ||
1133 par.isFreeSpacing();
1135 if (lyxrc.auto_number) {
1136 static string const number_operators = "+-/*";
1137 static string const number_unary_operators = "+-";
1138 static string const number_seperators = ".,:";
1140 if (current_font.number() == LyXFont::ON) {
1141 if (!IsDigit(c) && !contains(number_operators, c) &&
1142 !(contains(number_seperators, c) &&
1144 cur.pos() != cur.lastpos() &&
1145 getFont(par, cur.pos()).number() == LyXFont::ON &&
1146 getFont(par, cur.pos() - 1).number() == LyXFont::ON)
1148 number(cur); // Set current_font.number to OFF
1149 } else if (IsDigit(c) &&
1150 real_current_font.isVisibleRightToLeft()) {
1151 number(cur); // Set current_font.number to ON
1153 if (cur.pos() != 0) {
1154 char const c = par.getChar(cur.pos() - 1);
1155 if (contains(number_unary_operators, c) &&
1157 || par.isSeparator(cur.pos() - 2)
1158 || par.isNewline(cur.pos() - 2))
1160 setCharFont(pit, cur.pos() - 1, current_font);
1161 } else if (contains(number_seperators, c)
1163 && getFont(par, cur.pos() - 2).number() == LyXFont::ON) {
1164 setCharFont(pit, cur.pos() - 1, current_font);
1170 // First check, if there will be two blanks together or a blank at
1171 // the beginning of a paragraph.
1172 // I decided to handle blanks like normal characters, the main
1173 // difference are the special checks when calculating the row.fill
1174 // (blank does not count at the end of a row) and the check here
1176 // The bug is triggered when we type in a description environment:
1177 // The current_font is not changed when we go from label to main text
1178 // and it should (along with realtmpfont) when we type the space.
1179 // CHECK There is a bug here! (Asger)
1181 // store the current font. This is because of the use of cursor
1182 // movements. The moving cursor would refresh the current font
1183 LyXFont realtmpfont = real_current_font;
1184 LyXFont rawtmpfont = current_font;
1186 // When the free-spacing option is set for the current layout,
1187 // disable the double-space checking
1188 if (!freeSpacing && IsLineSeparatorChar(c)) {
1189 if (cur.pos() == 0) {
1190 static bool sent_space_message = false;
1191 if (!sent_space_message) {
1192 cur.message(_("You cannot insert a space at the "
1193 "beginning of a paragraph. Please read the Tutorial."));
1194 sent_space_message = true;
1198 BOOST_ASSERT(cur.pos() > 0);
1199 if (par.isLineSeparator(cur.pos() - 1)
1200 || par.isNewline(cur.pos() - 1)) {
1201 static bool sent_space_message = false;
1202 if (!sent_space_message) {
1203 cur.message(_("You cannot type two spaces this way. "
1204 "Please read the Tutorial."));
1205 sent_space_message = true;
1211 par.insertChar(cur.pos(), c, rawtmpfont);
1213 current_font = rawtmpfont;
1214 real_current_font = realtmpfont;
1216 setCursor(cur, cur.par(), cur.pos() + 1, false, cur.boundary());
1221 void LyXText::charInserted()
1223 // Here we call finishUndo for every 20 characters inserted.
1224 // This is from my experience how emacs does it. (Lgb)
1225 static unsigned int counter;
1235 RowMetrics LyXText::computeRowMetrics(par_type const pit, Row const & row) const
1238 Paragraph const & par = pars_[pit];
1240 double w = width_ - row.width();
1242 bool const is_rtl = isRTL(par);
1244 result.x = rightMargin(par);
1246 result.x = leftMargin(pit, row.pos());
1248 // is there a manual margin with a manual label
1249 LyXLayout_ptr const & layout = par.layout();
1251 if (layout->margintype == MARGIN_MANUAL
1252 && layout->labeltype == LABEL_MANUAL) {
1253 /// We might have real hfills in the label part
1254 int nlh = numberOfLabelHfills(par, row);
1256 // A manual label par (e.g. List) has an auto-hfill
1257 // between the label text and the body of the
1259 // But we don't want to do this auto hfill if the par
1264 if (nlh && !par.getLabelWidthString().empty())
1265 result.label_hfill = labelFill(par, row) / double(nlh);
1268 // are there any hfills in the row?
1269 int const nh = numberOfHfills(par, row);
1273 result.hfill = w / nh;
1274 // we don't have to look at the alignment if it is ALIGN_LEFT and
1275 // if the row is already larger then the permitted width as then
1276 // we force the LEFT_ALIGN'edness!
1277 } else if (int(row.width()) < maxwidth_) {
1278 // is it block, flushleft or flushright?
1279 // set x how you need it
1281 if (par.params().align() == LYX_ALIGN_LAYOUT)
1282 align = layout->align;
1284 align = par.params().align();
1286 // Display-style insets should always be on a centred row
1287 // The test on par.size() is to catch zero-size pars, which
1288 // would trigger the assert in Paragraph::getInset().
1289 //inset = par.size() ? par.getInset(row.pos()) : 0;
1291 && par.isInset(row.pos())
1292 && par.getInset(row.pos())->display())
1294 align = LYX_ALIGN_CENTER;
1298 case LYX_ALIGN_BLOCK: {
1299 int const ns = numberOfSeparators(par, row);
1300 bool disp_inset = false;
1301 if (row.endpos() < par.size()) {
1302 InsetBase const * in = par.getInset(row.endpos());
1304 disp_inset = in->display();
1306 // If we have separators, this is not the last row of a
1307 // par, does not end in newline, and is not row above a
1308 // display inset... then stretch it
1310 && row.endpos() < par.size()
1311 && !par.isNewline(row.endpos() - 1)
1314 result.separator = w / ns;
1315 } else if (is_rtl) {
1320 case LYX_ALIGN_RIGHT:
1323 case LYX_ALIGN_CENTER:
1329 bidi.computeTables(par, *bv()->buffer(), row);
1331 pos_type body_pos = par.beginOfBody();
1332 pos_type end = row.endpos();
1335 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1337 result.x += font_metrics::width(layout->labelsep, getLabelFont(par));
1338 if (body_pos <= end)
1339 result.x += result.label_hfill;
1347 // the cursor set functions have a special mechanism. When they
1348 // realize, that you left an empty paragraph, they will delete it.
1350 void LyXText::cursorRightOneWord(LCursor & cur)
1352 BOOST_ASSERT(this == cur.text());
1353 if (cur.pos() == cur.lastpos() && cur.par() != cur.lastpar()) {
1357 // Skip through initial nonword stuff.
1358 // Treat floats and insets as words.
1359 while (cur.pos() != cur.lastpos() && !cur.paragraph().isWord(cur.pos()))
1361 // Advance through word.
1362 while (cur.pos() != cur.lastpos() && cur.paragraph().isWord(cur.pos()))
1365 setCursor(cur, cur.par(), cur.pos());
1369 void LyXText::cursorLeftOneWord(LCursor & cur)
1371 BOOST_ASSERT(this == cur.text());
1372 if (cur.pos() == 0 && cur.par() != 0) {
1374 cur.pos() = cur.lastpos();
1376 // Skip through initial nonword stuff.
1377 // Treat floats and insets as words.
1378 while (cur.pos() != 0 && !cur.paragraph().isWord(cur.pos() - 1))
1380 // Advance through word.
1381 while (cur.pos() != 0 && cur.paragraph().isWord(cur.pos() - 1))
1384 setCursor(cur, cur.par(), cur.pos());
1388 void LyXText::selectWord(LCursor & cur, word_location loc)
1390 BOOST_ASSERT(this == cur.text());
1391 CursorSlice from = cur.top();
1392 CursorSlice to = cur.top();
1393 getWord(from, to, loc);
1394 if (cur.top() != from)
1395 setCursor(cur, from.par(), from.pos());
1399 setCursor(cur, to.par(), to.pos());
1404 // Select the word currently under the cursor when no
1405 // selection is currently set
1406 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1408 BOOST_ASSERT(this == cur.text());
1409 if (cur.selection())
1411 selectWord(cur, loc);
1412 return cur.selection();
1416 void LyXText::acceptChange(LCursor & cur)
1418 BOOST_ASSERT(this == cur.text());
1419 if (!cur.selection() && cur.lastpos() != 0)
1422 CursorSlice const & startc = cur.selBegin();
1423 CursorSlice const & endc = cur.selEnd();
1424 if (startc.par() == endc.par()) {
1425 recordUndoSelection(cur, Undo::INSERT);
1426 pars_[startc.par()].acceptChange(startc.pos(), endc.pos());
1428 cur.clearSelection();
1429 redoParagraph(startc.par());
1430 setCursorIntern(cur, startc.par(), 0);
1432 #ifdef WITH_WARNINGS
1433 #warning handle multi par selection
1438 void LyXText::rejectChange(LCursor & cur)
1440 BOOST_ASSERT(this == cur.text());
1441 if (!cur.selection() && cur.lastpos() != 0)
1444 CursorSlice const & startc = cur.selBegin();
1445 CursorSlice const & endc = cur.selEnd();
1446 if (startc.par() == endc.par()) {
1447 recordUndoSelection(cur, Undo::INSERT);
1448 pars_[startc.par()].rejectChange(startc.pos(), endc.pos());
1450 cur.clearSelection();
1451 redoParagraph(startc.par());
1452 setCursorIntern(cur, startc.par(), 0);
1454 #ifdef WITH_WARNINGS
1455 #warning handle multi par selection
1460 // Delete from cursor up to the end of the current or next word.
1461 void LyXText::deleteWordForward(LCursor & cur)
1463 BOOST_ASSERT(this == cur.text());
1464 if (cur.lastpos() == 0)
1468 cur.selection() = true;
1469 cursorRightOneWord(cur);
1471 cutSelection(cur, true, false);
1476 // Delete from cursor to start of current or prior word.
1477 void LyXText::deleteWordBackward(LCursor & cur)
1479 BOOST_ASSERT(this == cur.text());
1480 if (cur.lastpos() == 0)
1484 cur.selection() = true;
1485 cursorLeftOneWord(cur);
1487 cutSelection(cur, true, false);
1492 // Kill to end of line.
1493 void LyXText::deleteLineForward(LCursor & cur)
1495 BOOST_ASSERT(this == cur.text());
1496 if (cur.lastpos() == 0) {
1497 // Paragraph is empty, so we just go to the right
1501 cur.selection() = true; // to avoid deletion
1504 // What is this test for ??? (JMarc)
1505 if (!cur.selection())
1506 deleteWordForward(cur);
1508 cutSelection(cur, true, false);
1513 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1515 BOOST_ASSERT(this == cur.text());
1519 if (cur.selection()) {
1520 from = cur.selBegin();
1524 getWord(from, to, lyx::PARTIAL_WORD);
1525 setCursor(cur, to.par(), to.pos() + 1);
1528 recordUndoSelection(cur);
1530 pos_type pos = from.pos();
1531 int par = from.par();
1533 while (par != int(pars_.size()) && (pos != to.pos() || par != to.par())) {
1535 if (pos == pars_[pit].size()) {
1540 unsigned char c = pars_[pit].getChar(pos);
1541 if (c != Paragraph::META_INSET) {
1543 case text_lowercase:
1546 case text_capitalization:
1548 action = text_lowercase;
1550 case text_uppercase:
1555 #ifdef WITH_WARNINGS
1558 pars_[pit].setChar(pos, c);
1564 void LyXText::Delete(LCursor & cur)
1566 BOOST_ASSERT(this == cur.text());
1567 if (cur.pos() != cur.lastpos()) {
1568 recordUndo(cur, Undo::DELETE, cur.par());
1569 setCursorIntern(cur, cur.par(), cur.pos() + 1, false, cur.boundary());
1572 // should we do anything in an else branch?
1576 void LyXText::backspace(LCursor & cur)
1578 BOOST_ASSERT(this == cur.text());
1579 if (cur.pos() == 0) {
1580 // The cursor is at the beginning of a paragraph, so
1581 // the the backspace will collapse two paragraphs into
1584 // but it's not allowed unless it's new
1585 Paragraph & par = cur.paragraph();
1586 if (par.isChangeEdited(0, par.size()))
1589 // we may paste some paragraphs
1591 // is it an empty paragraph?
1592 pos_type lastpos = cur.lastpos();
1593 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1594 // This is an empty paragraph and we delete it just
1595 // by moving the cursor one step
1596 // left and let the DeleteEmptyParagraphMechanism
1597 // handle the actual deletion of the paragraph.
1599 if (cur.par() != 0) {
1601 // the layout things can change the height of a row !
1608 recordUndo(cur, Undo::DELETE, cur.par() - 1);
1610 par_type tmppit = cur.par();
1611 // We used to do cursorLeftIntern() here, but it is
1612 // not a good idea since it triggers the auto-delete
1613 // mechanism. So we do a cursorLeftIntern()-lite,
1614 // without the dreaded mechanism. (JMarc)
1615 if (cur.par() != 0) {
1616 // steps into the above paragraph.
1617 setCursorIntern(cur, cur.par() - 1,
1618 pars_[cur.par() - 1].size(),
1622 // Pasting is not allowed, if the paragraphs have different
1623 // layout. I think it is a real bug of all other
1624 // word processors to allow it. It confuses the user.
1625 // Correction: Pasting is always allowed with standard-layout
1626 Buffer & buf = cur.buffer();
1627 BufferParams const & bufparams = buf.params();
1628 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1629 par_type const cpit = cur.par();
1632 && (pars_[cpit].layout() == pars_[tmppit].layout()
1633 || pars_[tmppit].layout() == tclass.defaultLayout())
1634 && pars_[cpit].getAlign() == pars_[tmppit].getAlign()) {
1635 mergeParagraph(bufparams, buf.paragraphs(), cpit);
1637 if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
1640 // the counters may have changed
1642 setCursor(cur, cur.par(), cur.pos(), false);
1645 // this is the code for a normal backspace, not pasting
1647 recordUndo(cur, Undo::DELETE);
1648 // We used to do cursorLeftIntern() here, but it is
1649 // not a good idea since it triggers the auto-delete
1650 // mechanism. So we do a cursorLeftIntern()-lite,
1651 // without the dreaded mechanism. (JMarc)
1652 setCursorIntern(cur, cur.par(), cur.pos() - 1,
1653 false, cur.boundary());
1654 cur.paragraph().erase(cur.pos());
1657 if (cur.pos() == cur.lastpos())
1658 setCurrentFont(cur);
1661 setCursor(cur, cur.par(), cur.pos(), false, cur.boundary());
1665 Paragraph & LyXText::getPar(par_type par) const
1667 //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
1668 BOOST_ASSERT(par >= 0);
1669 BOOST_ASSERT(par < int(paragraphs().size()));
1670 return paragraphs()[par];
1674 Row const & LyXText::firstRow() const
1676 return *paragraphs().front().rows.begin();
1680 void LyXText::redoParagraphInternal(par_type const pit)
1682 // remove rows of paragraph, keep track of height changes
1683 Paragraph & par = pars_[pit];
1684 height_ -= par.height;
1692 InsetList::iterator ii = par.insetlist.begin();
1693 InsetList::iterator iend = par.insetlist.end();
1694 for (; ii != iend; ++ii) {
1696 int const w = maxwidth_ - leftMargin(pit) - rightMargin(par);
1697 MetricsInfo mi(bv(), getFont(par, ii->pos), w);
1698 ii->inset->metrics(mi, dim);
1701 // rebreak the paragraph
1702 par.setBeginOfBody();
1706 rowBreakPoint(pit, row);
1707 setRowWidth(pit, row);
1708 setHeightOfRow(pit, row);
1709 row.y_offset(par.height);
1710 par.rows.push_back(row);
1711 par.width = std::max(par.width, row.width());
1712 par.height += row.height();
1714 } while (z < par.size());
1716 height_ += par.height;
1717 //lyxerr << "redoParagraph: " << par.rows.size() << " rows\n";
1721 void LyXText::redoParagraphs(par_type pit, par_type end)
1723 for (; pit != end; ++pit)
1724 redoParagraphInternal(pit);
1725 updateParPositions();
1730 void LyXText::redoParagraph(par_type pit)
1732 redoParagraphInternal(pit);
1733 updateParPositions();
1737 void LyXText::fullRebreak()
1739 redoParagraphs(0, paragraphs().size());
1740 bv()->cursor().resetAnchor();
1744 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1746 //BOOST_ASSERT(mi.base.textwidth);
1747 if (mi.base.textwidth)
1748 maxwidth_ = mi.base.textwidth;
1749 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1750 //<< " maxWidth: " << maxwidth << "\nfont: " << mi.base.font
1753 // Rebuild row cache. This recomputes height as well.
1754 redoParagraphs(0, paragraphs().size());
1756 width_ = maxParagraphWidth(paragraphs());
1759 dim.asc = firstRow().ascent_of_text();
1760 dim.des = height_ - dim.asc;
1765 // only used for inset right now. should also be used for main text
1766 void LyXText::draw(PainterInfo & pi, int x, int y) const
1770 paintTextInset(*this, pi);
1774 // only used for inset right now. should also be used for main text
1775 void LyXText::drawSelection(PainterInfo &, int, int) const
1777 //lyxerr << "LyXText::drawSelection at " << x << " " << y << endl;
1781 bool LyXText::isLastRow(par_type pit, Row const & row) const
1783 return row.endpos() >= pars_[pit].size()
1784 && pit + 1 == par_type(paragraphs().size());
1788 bool LyXText::isFirstRow(par_type pit, Row const & row) const
1790 return row.pos() == 0 && pit == 0;
1794 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1795 word_location const loc)
1797 Paragraph const & from_par = pars_[from.par()];
1799 case lyx::WHOLE_WORD_STRICT:
1800 if (from.pos() == 0 || from.pos() == from_par.size()
1801 || !from_par.isWord(from.pos())
1802 || !from_par.isWord(from.pos() - 1)) {
1806 // no break here, we go to the next
1808 case lyx::WHOLE_WORD:
1809 // If we are already at the beginning of a word, do nothing
1810 if (!from.pos() || !from_par.isWord(from.pos() - 1))
1812 // no break here, we go to the next
1814 case lyx::PREVIOUS_WORD:
1815 // always move the cursor to the beginning of previous word
1816 while (from.pos() && from_par.isWord(from.pos() - 1))
1819 case lyx::NEXT_WORD:
1820 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1823 case lyx::PARTIAL_WORD:
1824 // no need to move the 'from' cursor
1828 Paragraph & to_par = pars_[to.par()];
1829 while (to.pos() < to_par.size() && to_par.isWord(to.pos()))
1834 void LyXText::write(Buffer const & buf, std::ostream & os) const
1836 ParagraphList::const_iterator pit = paragraphs().begin();
1837 ParagraphList::const_iterator end = paragraphs().end();
1838 Paragraph::depth_type dth = 0;
1839 for (; pit != end; ++pit)
1840 pit->write(buf, os, buf.params(), dth);
1844 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1846 static Change current_change;
1848 Paragraph::depth_type depth = 0;
1850 while (lex.isOK()) {
1852 string const token = lex.getString();
1857 if (token == "\\end_inset") {
1861 if (token == "\\end_body") {
1865 if (token == "\\begin_body") {
1869 if (token == "\\end_document") {
1873 if (token == "\\begin_layout") {
1874 lex.pushToken(token);
1877 par.params().depth(depth);
1878 if (buf.params().tracking_changes)
1880 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1881 pars_.push_back(par);
1883 // FIXME: goddamn InsetTabular makes us pass a Buffer
1885 ::readParagraph(buf, pars_.back(), lex);
1887 } else if (token == "\\begin_deeper") {
1889 } else if (token == "\\end_deeper") {
1891 lex.printError("\\end_deeper: " "depth is already null");
1896 lyxerr << "Handling unknown body token: `"
1897 << token << '\'' << endl;
1904 int LyXText::ascent() const
1906 return firstRow().ascent_of_text();
1910 int LyXText::descent() const
1912 return height_ - firstRow().ascent_of_text();
1916 int LyXText::cursorX(CursorSlice const & cur) const
1918 par_type const pit = cur.par();
1919 Paragraph const & par = pars_[pit];
1920 if (par.rows.empty())
1923 Row const & row = *par.getRow(cur.pos());
1925 pos_type pos = cur.pos();
1926 pos_type cursor_vpos = 0;
1928 RowMetrics const m = computeRowMetrics(pit, row);
1931 pos_type const row_pos = row.pos();
1932 pos_type const end = row.endpos();
1935 cursor_vpos = row_pos;
1936 else if (pos >= end)
1937 cursor_vpos = isRTL(par) ? row_pos : end;
1938 else if (pos > row_pos && pos >= end)
1939 // Place cursor after char at (logical) position pos - 1
1940 cursor_vpos = (bidi.level(pos - 1) % 2 == 0)
1941 ? bidi.log2vis(pos - 1) + 1 : bidi.log2vis(pos - 1);
1943 // Place cursor before char at (logical) position pos
1944 cursor_vpos = (bidi.level(pos) % 2 == 0)
1945 ? bidi.log2vis(pos) : bidi.log2vis(pos) + 1;
1947 pos_type body_pos = par.beginOfBody();
1949 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1952 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1953 pos_type pos = bidi.vis2log(vpos);
1954 if (body_pos > 0 && pos == body_pos - 1) {
1956 + font_metrics::width(par.layout()->labelsep,
1958 if (par.isLineSeparator(body_pos - 1))
1959 x -= singleWidth(par, body_pos - 1);
1962 if (hfillExpansion(par, row, pos)) {
1963 x += singleWidth(par, pos);
1964 if (pos >= body_pos)
1968 } else if (par.isSeparator(pos)) {
1969 x += singleWidth(par, pos);
1970 if (pos >= body_pos)
1973 x += singleWidth(par, pos);
1975 return xo_ + int(x);
1979 int LyXText::cursorY(CursorSlice const & cur) const
1981 Paragraph const & par = getPar(cur.par());
1982 Row const & row = *par.getRow(cur.pos());
1983 return yo_ + par.y + row.y_offset() + row.baseline();
1987 // Returns the current font and depth as a message.
1988 string LyXText::currentState(LCursor & cur)
1990 BOOST_ASSERT(this == cur.text());
1991 Buffer & buf = cur.buffer();
1992 Paragraph const & par = cur.paragraph();
1993 std::ostringstream os;
1995 bool const show_change = buf.params().tracking_changes
1996 && cur.pos() != cur.lastpos()
1997 && par.lookupChange(cur.pos()) != Change::UNCHANGED;
2000 Change change = par.lookupChangeFull(cur.pos());
2001 Author const & a = buf.params().authors().get(change.author);
2002 os << _("Change: ") << a.name();
2003 if (!a.email().empty())
2004 os << " (" << a.email() << ")";
2005 if (change.changetime)
2006 os << _(" at ") << ctime(&change.changetime);
2010 // I think we should only show changes from the default
2012 LyXFont font = real_current_font;
2013 font.reduce(buf.params().getLyXTextClass().defaultfont());
2015 // avoid _(...) re-entrance problem
2016 string const s = font.stateText(&buf.params());
2017 os << bformat(_("Font: %1$s"), s);
2019 // os << bformat(_("Font: %1$s"), font.stateText(&buf.params));
2021 // The paragraph depth
2022 int depth = cur.paragraph().getDepth();
2024 os << bformat(_(", Depth: %1$s"), tostr(depth));
2026 // The paragraph spacing, but only if different from
2028 Spacing const & spacing = par.params().spacing();
2029 if (!spacing.isDefault()) {
2030 os << _(", Spacing: ");
2031 switch (spacing.getSpace()) {
2032 case Spacing::Single:
2035 case Spacing::Onehalf:
2038 case Spacing::Double:
2041 case Spacing::Other:
2042 os << _("Other (") << spacing.getValue() << ')';
2044 case Spacing::Default:
2045 // should never happen, do nothing
2050 #ifdef DEVEL_VERSION
2051 os << _(", Inset: ") << &cur.inset();
2052 os << _(", Paragraph: ") << cur.par();
2053 os << _(", Id: ") << par.id();
2054 os << _(", Position: ") << cur.pos();
2055 Row & row = cur.textRow();
2056 os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2062 string LyXText::getPossibleLabel(LCursor & cur) const
2064 par_type pit = cur.par();
2066 LyXLayout_ptr layout = pars_[pit].layout();
2068 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2069 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
2070 if (layout2->latextype != LATEX_PARAGRAPH) {
2076 string text = layout->latexname().substr(0, 3);
2077 if (layout->latexname() == "theorem")
2078 text = "thm"; // Create a correct prefix for prettyref
2081 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2084 string par_text = pars_[pit].asString(cur.buffer(), false);
2085 for (int i = 0; i < lyxrc.label_init_length; ++i) {
2086 if (par_text.empty())
2089 par_text = split(par_text, head, ' ');
2090 // Is it legal to use spaces in labels ?
2100 // Manhattan distance to nearest corner
2101 int LyXText::dist(int x, int y) const
2108 else if (x > xo_ + int(width_))
2109 xx = x - xo_ - width_;
2111 if (y < yo_ - ascent())
2112 yy = yo_ - ascent() - y;
2113 else if (y > yo_ + descent())
2114 yy = y - yo_ - descent();
2116 lyxerr << " xo_=" << xo_ << " yo_=" << yo_
2117 << " width_=" << width_ << " ascent=" << ascent()
2118 << " descent=" << descent()
2119 << " dist=" << xx+yy <<endl;