3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * \author Jürgen Vigna
14 * Full author contact details are available in file CREDITS.
23 #include "buffer_funcs.h"
24 #include "bufferparams.h"
25 #include "BufferView.h"
27 #include "pariterator.h"
28 #include "coordcache.h"
29 #include "CutAndPaste.h"
31 #include "dispatchresult.h"
33 #include "errorlist.h"
34 #include "funcrequest.h"
36 #include "FontIterator.h"
40 #include "lyxlength.h"
44 #include "lyxrow_funcs.h"
45 #include "metricsinfo.h"
46 #include "paragraph.h"
47 #include "paragraph_funcs.h"
48 #include "ParagraphParameters.h"
49 #include "rowpainter.h"
52 #include "WordLangTuple.h"
54 #include "frontends/FontMetrics.h"
55 #include "frontends/Painter.h"
57 #include "insets/insettext.h"
58 #include "insets/insetbibitem.h"
59 #include "insets/insethfill.h"
60 #include "insets/insetlatexaccent.h"
61 #include "insets/insetline.h"
62 #include "insets/insetnewline.h"
63 #include "insets/insetpagebreak.h"
64 #include "insets/insetoptarg.h"
65 #include "insets/insetspace.h"
66 #include "insets/insetspecialchar.h"
67 #include "insets/insettabular.h"
69 #include "support/lstrings.h"
70 #include "support/textutils.h"
71 #include "support/convert.h"
73 #include <boost/current_function.hpp>
80 using support::bformat;
81 using support::contains;
82 using support::lowercase;
84 using support::uppercase;
86 using cap::cutSelection;
87 using cap::pasteParagraphList;
89 using frontend::FontMetrics;
102 int numberOfSeparators(Paragraph const & par, Row const & row)
104 pos_type const first = max(row.pos(), par.beginOfBody());
105 pos_type const last = row.endpos() - 1;
107 for (pos_type p = first; p < last; ++p) {
108 if (par.isSeparator(p))
115 int numberOfLabelHfills(Paragraph const & par, Row const & row)
117 pos_type last = row.endpos() - 1;
118 pos_type first = row.pos();
120 // hfill *DO* count at the beginning of paragraphs!
122 while (first < last && par.isHfill(first))
126 last = min(last, par.beginOfBody());
128 for (pos_type p = first; p < last; ++p) {
136 int numberOfHfills(Paragraph const & par, Row const & row)
138 pos_type const last = row.endpos();
139 pos_type first = row.pos();
141 // hfill *DO* count at the beginning of paragraphs!
143 while (first < last && par.isHfill(first))
147 first = max(first, par.beginOfBody());
150 for (pos_type p = first; p < last; ++p) {
158 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
159 string const & token, LyXFont & font, Change & change, ErrorList & errorList)
161 BufferParams const & bp = buf.params();
163 if (token[0] != '\\') {
165 string::const_iterator cit = token.begin();
166 for (; cit != token.end(); ++cit)
167 par.insertChar(par.size(), (*cit), font, change);
169 docstring dstr = lex.getDocString();
170 docstring::const_iterator cit = dstr.begin();
171 docstring::const_iterator cend = dstr.end();
172 for (; cit != cend; ++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);
180 change = Change(Change::UNCHANGED);
182 LyXTextClass const & tclass = bp.getLyXTextClass();
184 if (layoutname.empty()) {
185 layoutname = tclass.defaultLayoutName();
188 bool hasLayout = tclass.hasLayout(layoutname);
191 errorList.push_back(ErrorItem(_("Unknown layout"),
192 bformat(_("Layout '%1$s' does not exist in textclass '%2$s'\nTrying to use the default instead.\n"),
193 from_utf8(layoutname), from_utf8(tclass.name())), par.id(), 0, par.size()));
194 layoutname = tclass.defaultLayoutName();
197 par.layout(bp.getLyXTextClass()[layoutname]);
199 // Test whether the layout is obsolete.
200 LyXLayout_ptr const & layout = par.layout();
201 if (!layout->obsoleted_by().empty())
202 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
204 par.params().read(lex);
206 } else if (token == "\\end_layout") {
207 lyxerr << BOOST_CURRENT_FUNCTION
208 << ": Solitary \\end_layout in line "
209 << lex.getLineNo() << "\n"
210 << "Missing \\begin_layout?.\n";
211 } else if (token == "\\end_inset") {
212 lyxerr << BOOST_CURRENT_FUNCTION
213 << ": Solitary \\end_inset in line "
214 << lex.getLineNo() << "\n"
215 << "Missing \\begin_inset?.\n";
216 } else if (token == "\\begin_inset") {
217 InsetBase * inset = readInset(lex, buf);
219 par.insertInset(par.size(), inset, font, change);
222 docstring line = from_utf8(lex.getString());
223 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
224 par.id(), 0, par.size()));
226 } else if (token == "\\family") {
228 font.setLyXFamily(lex.getString());
229 } else if (token == "\\series") {
231 font.setLyXSeries(lex.getString());
232 } else if (token == "\\shape") {
234 font.setLyXShape(lex.getString());
235 } else if (token == "\\size") {
237 font.setLyXSize(lex.getString());
238 } else if (token == "\\lang") {
240 string const tok = lex.getString();
241 Language const * lang = languages.getLanguage(tok);
243 font.setLanguage(lang);
245 font.setLanguage(bp.language);
246 lex.printError("Unknown language `$$Token'");
248 } else if (token == "\\numeric") {
250 font.setNumber(font.setLyXMisc(lex.getString()));
251 } else if (token == "\\emph") {
253 font.setEmph(font.setLyXMisc(lex.getString()));
254 } else if (token == "\\bar") {
256 string const tok = lex.getString();
259 font.setUnderbar(LyXFont::ON);
260 else if (tok == "no")
261 font.setUnderbar(LyXFont::OFF);
262 else if (tok == "default")
263 font.setUnderbar(LyXFont::INHERIT);
265 lex.printError("Unknown bar font flag "
267 } else if (token == "\\noun") {
269 font.setNoun(font.setLyXMisc(lex.getString()));
270 } else if (token == "\\color") {
272 font.setLyXColor(lex.getString());
273 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
275 // Insets don't make sense in a free-spacing context! ---Kayvan
276 if (par.isFreeSpacing()) {
277 if (token == "\\InsetSpace")
278 par.insertChar(par.size(), ' ', font, change);
279 else if (lex.isOK()) {
281 string const next_token = lex.getString();
282 if (next_token == "\\-")
283 par.insertChar(par.size(), '-', font, change);
285 lex.printError("Token `$$Token' "
287 "paragraph layout!");
291 auto_ptr<InsetBase> inset;
292 if (token == "\\SpecialChar" )
293 inset.reset(new InsetSpecialChar);
295 inset.reset(new InsetSpace);
296 inset->read(buf, lex);
297 par.insertInset(par.size(), inset.release(),
300 } else if (token == "\\i") {
301 auto_ptr<InsetBase> inset(new InsetLatexAccent);
302 inset->read(buf, lex);
303 par.insertInset(par.size(), inset.release(), font, change);
304 } else if (token == "\\backslash") {
305 par.insertChar(par.size(), '\\', font, change);
306 } else if (token == "\\newline") {
307 auto_ptr<InsetBase> inset(new InsetNewline);
308 inset->read(buf, lex);
309 par.insertInset(par.size(), inset.release(), font, change);
310 } else if (token == "\\LyXTable") {
311 auto_ptr<InsetBase> inset(new InsetTabular(buf));
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: change tracking (MG)
323 // set empty 'par' to INSERTED???
324 change = Change(Change::UNCHANGED);
325 } else if (token == "\\change_inserted") {
327 std::istringstream is(lex.getString());
331 if (aid >= bp.author_map.size()) {
332 errorList.push_back(ErrorItem(_("Change tracking error"),
333 bformat(_("Unknown author index for insertion: %1$d\n"), aid),
334 par.id(), 0, par.size()));
336 change = Change(Change::UNCHANGED);
338 change = Change(Change::INSERTED, bp.author_map[aid], ct);
339 } else if (token == "\\change_deleted") {
341 std::istringstream is(lex.getString());
345 if (aid >= bp.author_map.size()) {
346 errorList.push_back(ErrorItem(_("Change tracking error"),
347 bformat(_("Unknown author index for deletion: %1$d\n"), aid),
348 par.id(), 0, par.size()));
350 change = Change(Change::UNCHANGED);
352 change = Change(Change::DELETED, bp.author_map[aid], ct);
355 errorList.push_back(ErrorItem(_("Unknown token"),
356 bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
357 from_utf8(lex.getString())),
358 par.id(), 0, par.size()));
363 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex,
364 ErrorList & errorList)
367 string token = lex.getString();
369 Change change(Change::UNCHANGED);
373 readParToken(buf, par, lex, token, font, change, errorList);
376 token = lex.getString();
381 if (token == "\\end_layout") {
382 //Ok, paragraph finished
386 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
387 << token << '\'' << endl;
388 if (token == "\\begin_layout" || token == "\\end_document"
389 || token == "\\end_inset" || token == "\\begin_deeper"
390 || token == "\\end_deeper") {
391 lex.pushToken(token);
392 lyxerr << "Paragraph ended in line "
393 << lex.getLineNo() << "\n"
394 << "Missing \\end_layout.\n";
398 // Final change goes to paragraph break:
399 par.setChange(par.size(), change);
401 // Initialize begin_of_body_ on load; redoParagraph maintains
402 par.setBeginOfBody();
410 BufferView * LyXText::bv() const
412 BOOST_ASSERT(bv_owner != 0);
417 double LyXText::spacing(Paragraph const & par) const
419 if (par.params().spacing().isDefault())
420 return bv()->buffer()->params().spacing().getValue();
421 return par.params().spacing().getValue();
425 int LyXText::width() const
431 int LyXText::height() const
433 return dim_.height();
437 int LyXText::singleWidth(Paragraph const & par, pos_type pos) const
439 return singleWidth(par, pos, par.getChar(pos), getFont(par, pos));
443 int LyXText::singleWidth(Paragraph const & par,
444 pos_type pos, char_type c, LyXFont const & font) const
446 // The most common case is handled first (Asger)
447 if (isPrintable(c)) {
448 Language const * language = font.language();
449 if (language->rightToLeft()) {
450 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
451 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
452 && language->lang() == "arabic") {
453 if (Encodings::isComposeChar_arabic(c))
455 c = par.transformChar(c, pos);
456 } else if (language->lang() == "hebrew" &&
457 Encodings::isComposeChar_hebrew(c))
460 return theFontMetrics(font).width(c);
463 if (c == Paragraph::META_INSET)
464 return par.getInset(pos)->width();
466 return theFontMetrics(font).width(c);
470 int LyXText::leftMargin(pit_type pit) const
472 BOOST_ASSERT(pit >= 0);
473 BOOST_ASSERT(pit < int(pars_.size()));
474 return leftMargin(pit, pars_[pit].size());
478 int LyXText::leftMargin(pit_type const pit, pos_type const pos) const
480 BOOST_ASSERT(pit >= 0);
481 BOOST_ASSERT(pit < int(pars_.size()));
482 Paragraph const & par = pars_[pit];
483 BOOST_ASSERT(pos >= 0);
484 BOOST_ASSERT(pos <= par.size());
485 //lyxerr << "LyXText::leftMargin: pit: " << pit << " pos: " << pos << endl;
486 BufferParams const & params = bv()->buffer()->params();
487 LyXTextClass const & tclass = params.getLyXTextClass();
488 LyXLayout_ptr const & layout = par.layout();
490 string parindent = layout->parindent;
495 l_margin += changebarMargin();
498 docstring leftm = from_utf8(tclass.leftmargin());
499 l_margin += theFontMetrics(params.getFont()).signedWidth(leftm);
501 if (par.getDepth() != 0) {
502 // find the next level paragraph
503 pit_type newpar = outerHook(pit, pars_);
504 if (newpar != pit_type(pars_.size())) {
505 if (pars_[newpar].layout()->isEnvironment()) {
506 l_margin = leftMargin(newpar);
508 if (par.layout() == tclass.defaultLayout()) {
509 if (pars_[newpar].params().noindent())
512 parindent = pars_[newpar].layout()->parindent;
517 // This happens after sections in standard classes. The 1.3.x
518 // code compared depths too, but it does not seem necessary
520 if (par.layout() == tclass.defaultLayout()
521 && pit > 0 && pars_[pit - 1].layout()->nextnoindent)
524 LyXFont const labelfont = getLabelFont(par);
525 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
527 switch (layout->margintype) {
529 if (!layout->leftmargin.empty()) {
531 docstring leftm = from_utf8(layout->leftmargin);
532 l_margin += theFontMetrics(params.getFont()).signedWidth(leftm);
534 if (!par.getLabelstring().empty()) {
536 docstring labin = from_utf8(layout->labelindent);
537 l_margin += labelfont_metrics.signedWidth(labin);
538 docstring labstr = par.getLabelstring();
539 l_margin += labelfont_metrics.width(labstr);
540 docstring labsep = from_utf8(layout->labelsep);
541 l_margin += labelfont_metrics.width(labsep);
545 case MARGIN_MANUAL: {
547 docstring labin = from_utf8(layout->labelindent);
548 l_margin += labelfont_metrics.signedWidth(labin);
549 // The width of an empty par, even with manual label, should be 0
550 if (!par.empty() && pos >= par.beginOfBody()) {
551 if (!par.getLabelWidthString().empty()) {
552 docstring labstr = par.getLabelWidthString();
553 l_margin += labelfont_metrics.width(labstr);
554 docstring labsep = from_utf8(layout->labelsep);
555 l_margin += labelfont_metrics.width(labsep);
561 case MARGIN_STATIC: {
563 docstring leftm = from_utf8(layout->leftmargin);
564 l_margin += theFontMetrics(params.getFont()).signedWidth(leftm)
565 * 4 / (par.getDepth() + 4);
569 case MARGIN_FIRST_DYNAMIC:
570 if (layout->labeltype == LABEL_MANUAL) {
571 if (pos >= par.beginOfBody()) {
573 l_margin += labelfont_metrics.signedWidth(
574 from_utf8(layout->leftmargin));
577 l_margin += labelfont_metrics.signedWidth(
578 from_utf8(layout->labelindent));
581 // Special case to fix problems with
583 || (layout->labeltype == LABEL_STATIC
584 && layout->latextype == LATEX_ENVIRONMENT
585 && !isFirstInSequence(pit, pars_))) {
587 l_margin += labelfont_metrics.signedWidth(from_utf8(layout->leftmargin));
588 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
589 && layout->labeltype != LABEL_BIBLIO
590 && layout->labeltype !=
591 LABEL_CENTERED_TOP_ENVIRONMENT) {
592 l_margin += labelfont_metrics.signedWidth(from_utf8(layout->labelindent));
593 l_margin += labelfont_metrics.width(from_utf8(layout->labelsep));
594 l_margin += labelfont_metrics.width(par.getLabelstring());
598 case MARGIN_RIGHT_ADDRESS_BOX: {
600 // ok, a terrible hack. The left margin depends on the widest
601 // row in this paragraph.
602 RowList::iterator rit = par.rows().begin();
603 RowList::iterator end = par.rows().end();
605 #warning This is wrong.
607 int minfill = maxwidth_;
608 for ( ; rit != end; ++rit)
609 if (rit->fill() < minfill)
610 minfill = rit->fill();
611 l_margin += theFontMetrics(params.getFont()).signedWidth(layout->leftmargin);
614 // also wrong, but much shorter.
615 l_margin += maxwidth_ / 2;
620 if (!par.params().leftIndent().zero())
621 l_margin += par.params().leftIndent().inPixels(maxwidth_);
625 if (par.params().align() == LYX_ALIGN_LAYOUT)
626 align = layout->align;
628 align = par.params().align();
630 // set the correct parindent
632 && (layout->labeltype == LABEL_NO_LABEL
633 || layout->labeltype == LABEL_TOP_ENVIRONMENT
634 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
635 || (layout->labeltype == LABEL_STATIC
636 && layout->latextype == LATEX_ENVIRONMENT
637 && !isFirstInSequence(pit, pars_)))
638 && align == LYX_ALIGN_BLOCK
639 && !par.params().noindent()
640 // in some insets, paragraphs are never indented
641 && !(par.inInset() && par.inInset()->neverIndent())
642 // display style insets are always centered, omit indentation
645 && par.getInset(pos)->display())
646 && (par.layout() != tclass.defaultLayout()
647 || bv()->buffer()->params().paragraph_separation ==
648 BufferParams::PARSEP_INDENT))
650 docstring din = from_utf8(parindent);
651 l_margin += theFontMetrics(params.getFont()).signedWidth(din);
658 int LyXText::rightMargin(Paragraph const & par) const
660 // FIXME: the correct way is to only call rightMargin() only
661 // within the main LyXText. The following test is thus bogus.
662 LyXText const & text = bv()->buffer()->text();
663 // We do not want rightmargins on inner texts.
667 BufferParams const & params = bv()->buffer()->params();
668 LyXTextClass const & tclass = params.getLyXTextClass();
669 docstring trmarg = from_utf8(tclass.rightmargin());
670 docstring lrmarg = from_utf8(par.layout()->rightmargin);
671 FontMetrics const & fm = theFontMetrics(params.getFont());
674 + fm.signedWidth(trmarg)
675 + fm.signedWidth(lrmarg)
676 * 4 / (par.getDepth() + 4);
682 int LyXText::labelEnd(pit_type const pit) const
684 // labelEnd is only needed if the layout fills a flushleft label.
685 if (pars_[pit].layout()->margintype != MARGIN_MANUAL)
687 // return the beginning of the body
688 return leftMargin(pit);
694 // this needs special handling - only newlines count as a break point
695 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
697 pos_type const end = par.size();
700 if (par.isNewline(i))
709 void LyXText::rowBreakPoint(pit_type const pit, Row & row) const
711 Paragraph const & par = pars_[pit];
712 pos_type const end = par.size();
713 pos_type const pos = row.pos();
719 // maximum pixel width of a row
720 int width = maxwidth_ - rightMargin(par); // - leftMargin(pit, row);
726 LyXLayout_ptr const & layout = par.layout();
728 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
729 row.endpos(addressBreakPoint(pos, par));
733 pos_type const body_pos = par.beginOfBody();
736 // Now we iterate through until we reach the right margin
737 // or the end of the par, then choose the possible break
740 int const left = leftMargin(pit, pos);
743 // pixel width since last breakpoint
746 FontIterator fi = FontIterator(*this, par, pos);
747 pos_type point = end;
749 FontMetrics const & fm = theFontMetrics(getLabelFont(par));
750 for ( ; i < end; ++i, ++fi) {
751 char_type const c = par.getChar(i);
752 int thiswidth = singleWidth(par, i, c, *fi);
754 // add the auto-hfill from label end to the body
755 if (body_pos && i == body_pos) {
756 docstring lsep = from_utf8(layout->labelsep);
757 int add = fm.width(lsep);
758 if (par.isLineSeparator(i - 1))
759 add -= singleWidth(par, i - 1);
761 add = std::max(add, labelEnd(pit) - x);
766 chunkwidth += thiswidth;
768 // break before a character that will fall off
769 // the right of the row
771 // if no break before, break here
772 if (point == end || chunkwidth >= width - left) {
779 // exit on last registered breakpoint:
783 if (par.isNewline(i)) {
789 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
794 if (par.isInset(i) && par.getInset(i)->display()) {
800 if (!par.isInset(i) || par.getInset(i)->isChar()) {
801 // some insets are line separators too
802 if (par.isLineSeparator(i)) {
803 // register breakpoint:
810 // maybe found one, but the par is short enough.
811 if (i == end && x < width)
814 // manual labels cannot be broken in LaTeX. But we
815 // want to make our on-screen rendering of footnotes
817 if (body_pos && point < body_pos)
824 void LyXText::setRowWidth(pit_type const pit, Row & row) const
826 // get the pure distance
827 pos_type const end = row.endpos();
829 Paragraph const & par = pars_[pit];
830 docstring const labelsep = from_utf8(par.layout()->labelsep);
831 int w = leftMargin(pit, row.pos());
833 pos_type const body_pos = par.beginOfBody();
834 pos_type i = row.pos();
836 FontMetrics const & fm = theFontMetrics(getLabelFont(par));
839 FontIterator fi = FontIterator(*this, par, i);
840 for ( ; i < end; ++i, ++fi) {
841 if (body_pos > 0 && i == body_pos) {
842 w += fm.width(labelsep);
843 if (par.isLineSeparator(i - 1))
844 w -= singleWidth(par, i - 1);
845 w = max(w, labelEnd(pit));
847 char_type const c = par.getChar(i);
848 w += singleWidth(par, i, c, *fi);
852 if (body_pos > 0 && body_pos >= end) {
853 w += fm.width(labelsep);
854 if (end > 0 && par.isLineSeparator(end - 1))
855 w -= singleWidth(par, end - 1);
856 w = max(w, labelEnd(pit));
859 row.width(w + rightMargin(par));
863 // returns the minimum space a manual label needs on the screen in pixel
864 int LyXText::labelFill(Paragraph const & par, Row const & row) const
866 pos_type last = par.beginOfBody();
868 BOOST_ASSERT(last > 0);
870 // -1 because a label ends with a space that is in the label
873 // a separator at this end does not count
874 if (par.isLineSeparator(last))
878 for (pos_type i = row.pos(); i <= last; ++i)
879 w += singleWidth(par, i);
881 docstring const & label = par.params().labelWidthString();
885 FontMetrics const & fm = theFontMetrics(getLabelFont(par));
887 return max(0, fm.width(label) - w);
891 LColor_color LyXText::backgroundColor() const
893 return LColor_color(LColor::color(background_color_));
897 void LyXText::setHeightOfRow(pit_type const pit, Row & row)
899 Paragraph const & par = pars_[pit];
900 // get the maximum ascent and the maximum descent
901 double layoutasc = 0;
902 double layoutdesc = 0;
903 double const dh = defaultRowHeight();
905 // ok, let us initialize the maxasc and maxdesc value.
906 // Only the fontsize count. The other properties
907 // are taken from the layoutfont. Nicer on the screen :)
908 LyXLayout_ptr const & layout = par.layout();
910 // as max get the first character of this row then it can
911 // increase but not decrease the height. Just some point to
912 // start with so we don't have to do the assignment below too
914 LyXFont font = getFont(par, row.pos());
915 LyXFont::FONT_SIZE const tmpsize = font.size();
916 font = getLayoutFont(pit);
917 LyXFont::FONT_SIZE const size = font.size();
918 font.setSize(tmpsize);
920 LyXFont labelfont = getLabelFont(par);
922 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
923 FontMetrics const & fontmetrics = theFontMetrics(font);
925 // these are minimum values
926 double const spacing_val = layout->spacing.getValue() * spacing(par);
927 //lyxerr << "spacing_val = " << spacing_val << endl;
928 int maxasc = int(fontmetrics.maxAscent() * spacing_val);
929 int maxdesc = int(fontmetrics.maxDescent() * spacing_val);
931 // insets may be taller
932 InsetList::const_iterator ii = par.insetlist.begin();
933 InsetList::const_iterator iend = par.insetlist.end();
934 for ( ; ii != iend; ++ii) {
935 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
936 maxasc = max(maxasc, ii->inset->ascent());
937 maxdesc = max(maxdesc, ii->inset->descent());
941 // Check if any custom fonts are larger (Asger)
942 // This is not completely correct, but we can live with the small,
943 // cosmetic error for now.
945 pos_type const pos_end = row.endpos();
947 LyXFont::FONT_SIZE maxsize =
948 par.highestFontInRange(row.pos(), pos_end, size);
949 if (maxsize > font.size()) {
950 font.setSize(maxsize);
951 maxasc = max(maxasc, fontmetrics.maxAscent());
952 maxdesc = max(maxdesc, fontmetrics.maxDescent());
955 // This is nicer with box insets:
962 if (row.pos() == 0) {
963 BufferParams const & bufparams = bv()->buffer()->params();
964 // some parksips VERY EASY IMPLEMENTATION
965 if (bv()->buffer()->params().paragraph_separation
966 == BufferParams::PARSEP_SKIP
968 && ((layout->isParagraph() && par.getDepth() == 0)
969 || (pars_[pit - 1].layout()->isParagraph()
970 && pars_[pit - 1].getDepth() == 0)))
972 maxasc += bufparams.getDefSkip().inPixels(*bv());
975 if (par.params().startOfAppendix())
976 maxasc += int(3 * dh);
978 // This is special code for the chapter, since the label of this
979 // layout is printed in an extra row
980 if (layout->counter == "chapter"
981 && !par.params().labelString().empty()) {
982 labeladdon = int(labelfont_metrics.maxHeight()
983 * layout->spacing.getValue()
987 // special code for the top label
988 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
989 || layout->labeltype == LABEL_BIBLIO
990 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
991 && isFirstInSequence(pit, paragraphs())
992 && !par.getLabelstring().empty())
995 labelfont_metrics.maxHeight()
996 * layout->spacing.getValue()
998 + (layout->topsep + layout->labelbottomsep) * dh);
1001 // Add the layout spaces, for example before and after
1002 // a section, or between the items of a itemize or enumerate
1005 pit_type prev = depthHook(pit, pars_, par.getDepth());
1007 && pars_[prev].layout() == layout
1008 && pars_[prev].getDepth() == par.getDepth()
1009 && pars_[prev].getLabelWidthString() == par.getLabelWidthString())
1011 layoutasc = layout->itemsep * dh;
1012 } else if (pit != 0 || row.pos() != 0) {
1013 if (layout->topsep > 0)
1014 layoutasc = layout->topsep * dh;
1017 prev = outerHook(pit, pars_);
1018 if (prev != pit_type(pars_.size())) {
1019 maxasc += int(pars_[prev].layout()->parsep * dh);
1020 } else if (pit != 0) {
1021 if (pars_[pit - 1].getDepth() != 0 ||
1022 pars_[pit - 1].layout() == layout) {
1023 maxasc += int(layout->parsep * dh);
1028 // is it a bottom line?
1029 if (row.endpos() >= par.size()) {
1030 // add the layout spaces, for example before and after
1031 // a section, or between the items of a itemize or enumerate
1033 pit_type nextpit = pit + 1;
1034 if (nextpit != pit_type(pars_.size())) {
1035 pit_type cpit = pit;
1039 if (pars_[cpit].getDepth() > pars_[nextpit].getDepth()) {
1040 usual = pars_[cpit].layout()->bottomsep * dh;
1041 cpit = depthHook(cpit, paragraphs(), pars_[nextpit].getDepth());
1042 if (pars_[cpit].layout() != pars_[nextpit].layout()
1043 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1045 unusual = pars_[cpit].layout()->bottomsep * dh;
1047 layoutdesc = max(unusual, usual);
1048 } else if (pars_[cpit].getDepth() == pars_[nextpit].getDepth()) {
1049 if (pars_[cpit].layout() != pars_[nextpit].layout()
1050 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1051 layoutdesc = int(pars_[cpit].layout()->bottomsep * dh);
1056 // incalculate the layout spaces
1057 maxasc += int(layoutasc * 2 / (2 + pars_[pit].getDepth()));
1058 maxdesc += int(layoutdesc * 2 / (2 + pars_[pit].getDepth()));
1060 // FIXME: the correct way is to do the following is to move the
1061 // following code in another method specially tailored for the
1062 // main LyXText. The following test is thus bogus.
1063 LyXText const & text = bv_owner->buffer()->text();
1064 // Top and bottom margin of the document (only at top-level)
1065 if (&text == this) {
1066 if (pit == 0 && row.pos() == 0)
1068 if (pit + 1 == pit_type(pars_.size()) &&
1069 row.endpos() == par.size())
1073 row.ascent(maxasc + labeladdon);
1074 row.descent(maxdesc);
1082 void LyXText::breakParagraph(LCursor & cur, bool keep_layout)
1084 BOOST_ASSERT(this == cur.text());
1086 Paragraph & cpar = cur.paragraph();
1087 pit_type cpit = cur.pit();
1089 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
1090 LyXLayout_ptr const & layout = cpar.layout();
1092 // this is only allowed, if the current paragraph is not empty
1093 // or caption and if it has not the keepempty flag active
1094 if (cur.lastpos() == 0 && !cpar.allowEmpty()
1095 && layout->labeltype != LABEL_SENSITIVE)
1098 // a layout change may affect also the following paragraph
1099 recUndo(cur.pit(), undoSpan(cur.pit()) - 1);
1101 // Always break behind a space
1102 // It is better to erase the space (Dekel)
1103 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
1104 // FIXME: change tracking (MG)
1105 cpar.erase(cur.pos(), cur.buffer().params().trackChanges);
1107 // How should the layout for the new paragraph be?
1108 int preserve_layout = 0;
1110 preserve_layout = 2;
1112 preserve_layout = layout->isEnvironment();
1114 // We need to remember this before we break the paragraph, because
1115 // that invalidates the layout variable
1116 bool sensitive = layout->labeltype == LABEL_SENSITIVE;
1118 // we need to set this before we insert the paragraph.
1119 bool const isempty = cpar.allowEmpty() && cpar.empty();
1121 lyx::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
1122 cur.pos(), preserve_layout);
1124 // After this, neither paragraph contains any rows!
1127 pit_type next_par = cpit + 1;
1129 // well this is the caption hack since one caption is really enough
1132 // set to standard-layout
1133 pars_[cpit].applyLayout(tclass.defaultLayout());
1135 // set to standard-layout
1136 pars_[next_par].applyLayout(tclass.defaultLayout());
1139 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
1140 // FIXME: change tracking (MG)
1141 pars_[next_par].erase(0, cur.buffer().params().trackChanges);
1143 ParIterator current_it(cur);
1144 ParIterator last_it(cur);
1148 updateLabels(cur.buffer(), current_it, last_it);
1150 // Mark "carriage return" as inserted if change tracking:
1151 if (cur.buffer().params().trackChanges) {
1152 // FIXME: Change tracking (MG)
1153 cur.paragraph().setChange(cur.paragraph().size(),
1154 Change(Change::INSERTED));
1157 // This check is necessary. Otherwise the new empty paragraph will
1158 // be deleted automatically. And it is more friendly for the user!
1159 if (cur.pos() != 0 || isempty)
1160 setCursor(cur, cur.pit() + 1, 0);
1162 setCursor(cur, cur.pit(), 0);
1166 // insert a character, moves all the following breaks in the
1167 // same Paragraph one to the right and make a rebreak
1168 void LyXText::insertChar(LCursor & cur, char_type c)
1170 BOOST_ASSERT(this == cur.text());
1171 BOOST_ASSERT(c != Paragraph::META_INSET);
1173 recordUndo(cur, Undo::INSERT);
1175 Paragraph & par = cur.paragraph();
1176 // try to remove this
1177 pit_type const pit = cur.pit();
1179 bool const freeSpacing = par.layout()->free_spacing ||
1180 par.isFreeSpacing();
1182 if (lyxrc.auto_number) {
1183 static docstring const number_operators = from_ascii("+-/*");
1184 static docstring const number_unary_operators = from_ascii("+-");
1185 static docstring const number_seperators = from_ascii(".,:");
1187 if (current_font.number() == LyXFont::ON) {
1188 if (!isDigit(c) && !contains(number_operators, c) &&
1189 !(contains(number_seperators, c) &&
1191 cur.pos() != cur.lastpos() &&
1192 getFont(par, cur.pos()).number() == LyXFont::ON &&
1193 getFont(par, cur.pos() - 1).number() == LyXFont::ON)
1195 number(cur); // Set current_font.number to OFF
1196 } else if (isDigit(c) &&
1197 real_current_font.isVisibleRightToLeft()) {
1198 number(cur); // Set current_font.number to ON
1200 if (cur.pos() != 0) {
1201 char_type const c = par.getChar(cur.pos() - 1);
1202 if (contains(number_unary_operators, c) &&
1204 || par.isSeparator(cur.pos() - 2)
1205 || par.isNewline(cur.pos() - 2))
1207 setCharFont(pit, cur.pos() - 1, current_font);
1208 } else if (contains(number_seperators, c)
1210 && getFont(par, cur.pos() - 2).number() == LyXFont::ON) {
1211 setCharFont(pit, cur.pos() - 1, current_font);
1217 // First check, if there will be two blanks together or a blank at
1218 // the beginning of a paragraph.
1219 // I decided to handle blanks like normal characters, the main
1220 // difference are the special checks when calculating the row.fill
1221 // (blank does not count at the end of a row) and the check here
1223 // When the free-spacing option is set for the current layout,
1224 // disable the double-space checking
1225 if (!freeSpacing && isLineSeparatorChar(c)) {
1226 if (cur.pos() == 0) {
1227 static bool sent_space_message = false;
1228 if (!sent_space_message) {
1229 cur.message(_("You cannot insert a space at the "
1230 "beginning of a paragraph. Please read the Tutorial."));
1231 sent_space_message = true;
1235 BOOST_ASSERT(cur.pos() > 0);
1236 if ((par.isLineSeparator(cur.pos() - 1)
1237 || par.isNewline(cur.pos() - 1))
1238 // FIXME: change tracking (MG)
1239 && par.lookupChange(cur.pos() - 1) != Change(Change::DELETED)) {
1240 static bool sent_space_message = false;
1241 if (!sent_space_message) {
1242 cur.message(_("You cannot type two spaces this way. "
1243 "Please read the Tutorial."));
1244 sent_space_message = true;
1250 // FIXME: change tracking (MG)
1251 par.insertChar(cur.pos(), c, current_font, Change(Change::INSERTED));
1252 setCursor(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1257 void LyXText::charInserted()
1259 // Here we call finishUndo for every 20 characters inserted.
1260 // This is from my experience how emacs does it. (Lgb)
1261 static unsigned int counter;
1272 LyXText::computeRowMetrics(pit_type const pit, Row const & row) const
1275 Paragraph const & par = pars_[pit];
1277 double w = dim_.wid - row.width();
1279 bool const is_rtl = isRTL(par);
1281 result.x = rightMargin(par);
1283 result.x = leftMargin(pit, row.pos());
1285 // is there a manual margin with a manual label
1286 LyXLayout_ptr const & layout = par.layout();
1288 if (layout->margintype == MARGIN_MANUAL
1289 && layout->labeltype == LABEL_MANUAL) {
1290 /// We might have real hfills in the label part
1291 int nlh = numberOfLabelHfills(par, row);
1293 // A manual label par (e.g. List) has an auto-hfill
1294 // between the label text and the body of the
1296 // But we don't want to do this auto hfill if the par
1301 if (nlh && !par.getLabelWidthString().empty())
1302 result.label_hfill = labelFill(par, row) / double(nlh);
1305 // are there any hfills in the row?
1306 int const nh = numberOfHfills(par, row);
1310 result.hfill = w / nh;
1311 // we don't have to look at the alignment if it is ALIGN_LEFT and
1312 // if the row is already larger then the permitted width as then
1313 // we force the LEFT_ALIGN'edness!
1314 } else if (int(row.width()) < maxwidth_) {
1315 // is it block, flushleft or flushright?
1316 // set x how you need it
1318 if (par.params().align() == LYX_ALIGN_LAYOUT)
1319 align = layout->align;
1321 align = par.params().align();
1323 // Display-style insets should always be on a centred row
1324 // The test on par.size() is to catch zero-size pars, which
1325 // would trigger the assert in Paragraph::getInset().
1326 //inset = par.size() ? par.getInset(row.pos()) : 0;
1328 && par.isInset(row.pos())
1329 && par.getInset(row.pos())->display())
1331 align = LYX_ALIGN_CENTER;
1335 case LYX_ALIGN_BLOCK: {
1336 int const ns = numberOfSeparators(par, row);
1337 bool disp_inset = false;
1338 if (row.endpos() < par.size()) {
1339 InsetBase const * in = par.getInset(row.endpos());
1341 disp_inset = in->display();
1343 // If we have separators, this is not the last row of a
1344 // par, does not end in newline, and is not row above a
1345 // display inset... then stretch it
1347 && row.endpos() < par.size()
1348 && !par.isNewline(row.endpos() - 1)
1351 result.separator = w / ns;
1352 } else if (is_rtl) {
1357 case LYX_ALIGN_RIGHT:
1360 case LYX_ALIGN_CENTER:
1366 bidi.computeTables(par, *bv()->buffer(), row);
1368 pos_type body_pos = par.beginOfBody();
1369 pos_type end = row.endpos();
1372 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1374 docstring const lsep = from_utf8(layout->labelsep);
1375 result.x += theFontMetrics(getLabelFont(par)).width(lsep);
1376 if (body_pos <= end)
1377 result.x += result.label_hfill;
1385 // the cursor set functions have a special mechanism. When they
1386 // realize, that you left an empty paragraph, they will delete it.
1388 bool LyXText::cursorRightOneWord(LCursor & cur)
1390 BOOST_ASSERT(this == cur.text());
1394 if (old.pos() == old.lastpos() && old.pit() != old.lastpit()) {
1398 // Advance through word.
1399 while (old.pos() != old.lastpos() && old.paragraph().isLetter(old.pos()))
1401 // Skip through trailing nonword stuff.
1402 while (old.pos() != old.lastpos() && !old.paragraph().isLetter(old.pos()))
1405 return setCursor(cur, old.pit(), old.pos());
1409 bool LyXText::cursorLeftOneWord(LCursor & cur)
1411 BOOST_ASSERT(this == cur.text());
1415 if (old.pos() == 0 && old.pit() != 0) {
1417 old.pos() = old.lastpos();
1419 // Skip through initial nonword stuff.
1420 while (old.pos() != 0 && !old.paragraph().isLetter(old.pos() - 1))
1422 // Advance through word.
1423 while (old.pos() != 0 && old.paragraph().isLetter(old.pos() - 1))
1426 return setCursor(cur, old.pit(), old.pos());
1430 void LyXText::selectWord(LCursor & cur, word_location loc)
1432 BOOST_ASSERT(this == cur.text());
1433 CursorSlice from = cur.top();
1434 CursorSlice to = cur.top();
1435 getWord(from, to, loc);
1436 if (cur.top() != from)
1437 setCursor(cur, from.pit(), from.pos());
1441 setCursor(cur, to.pit(), to.pos());
1446 // Select the word currently under the cursor when no
1447 // selection is currently set
1448 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1450 BOOST_ASSERT(this == cur.text());
1451 if (cur.selection())
1453 selectWord(cur, loc);
1454 return cur.selection();
1458 void LyXText::acceptChange(LCursor & cur)
1460 BOOST_ASSERT(this == cur.text());
1461 if (!cur.selection() && cur.lastpos() != 0)
1464 recordUndoSelection(cur, Undo::INSERT);
1466 DocIterator it = cur.selectionBegin();
1467 DocIterator et = cur.selectionEnd();
1468 pit_type pit = it.pit();
1469 Change::Type const type = pars_[pit].lookupChange(it.pos()).type;
1470 for (; pit <= et.pit(); ++pit) {
1471 pos_type left = ( pit == it.pit() ? it.pos() : 0 );
1473 ( pit == et.pit() ? et.pos() : pars_[pit].size() + 1 );
1474 pars_[pit].acceptChange(left, right);
1476 if (type == Change::DELETED) {
1477 ParagraphList & plist = paragraphs();
1478 if (it.pit() + 1 < et.pit())
1479 pars_.erase(boost::next(plist.begin(), it.pit() + 1),
1480 boost::next(plist.begin(), et.pit()));
1482 // Paragraph merge if appropriate:
1483 // FIXME: change tracking (MG)
1484 if (pars_[it.pit()].lookupChange(pars_[it.pit()].size())
1485 == Change(Change::DELETED)) {
1486 setCursorIntern(cur, it.pit() + 1, 0);
1491 cur.clearSelection();
1492 setCursorIntern(cur, it.pit(), 0);
1496 void LyXText::rejectChange(LCursor & cur)
1498 BOOST_ASSERT(this == cur.text());
1499 if (!cur.selection() && cur.lastpos() != 0)
1502 recordUndoSelection(cur, Undo::INSERT);
1504 DocIterator it = cur.selectionBegin();
1505 DocIterator et = cur.selectionEnd();
1506 pit_type pit = it.pit();
1507 Change::Type const type = pars_[pit].lookupChange(it.pos()).type;
1508 for (; pit <= et.pit(); ++pit) {
1509 pos_type left = ( pit == it.pit() ? it.pos() : 0 );
1511 ( pit == et.pit() ? et.pos() : pars_[pit].size() + 1 );
1512 pars_[pit].rejectChange(left, right);
1514 if (type == Change::INSERTED) {
1515 ParagraphList & plist = paragraphs();
1516 if (it.pit() + 1 < et.pit())
1517 pars_.erase(boost::next(plist.begin(), it.pit() + 1),
1518 boost::next(plist.begin(), et.pit()));
1519 // Paragraph merge if appropriate:
1520 // FIXME: change tracking (MG)
1521 if (pars_[it.pit()].lookupChange(pars_[it.pit()].size())
1522 == Change(Change::INSERTED)) {
1523 setCursorIntern(cur, it.pit() + 1, 0);
1528 cur.clearSelection();
1529 setCursorIntern(cur, it.pit(), 0);
1533 // Delete from cursor up to the end of the current or next word.
1534 void LyXText::deleteWordForward(LCursor & cur)
1536 BOOST_ASSERT(this == cur.text());
1537 if (cur.lastpos() == 0)
1541 cur.selection() = true;
1542 cursorRightOneWord(cur);
1544 cutSelection(cur, true, false);
1549 // Delete from cursor to start of current or prior word.
1550 void LyXText::deleteWordBackward(LCursor & cur)
1552 BOOST_ASSERT(this == cur.text());
1553 if (cur.lastpos() == 0)
1557 cur.selection() = true;
1558 cursorLeftOneWord(cur);
1560 cutSelection(cur, true, false);
1565 // Kill to end of line.
1566 void LyXText::deleteLineForward(LCursor & cur)
1568 BOOST_ASSERT(this == cur.text());
1569 if (cur.lastpos() == 0) {
1570 // Paragraph is empty, so we just go to the right
1574 cur.selection() = true; // to avoid deletion
1577 // What is this test for ??? (JMarc)
1578 if (!cur.selection())
1579 deleteWordForward(cur);
1581 cutSelection(cur, true, false);
1586 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1588 BOOST_ASSERT(this == cur.text());
1592 if (cur.selection()) {
1593 from = cur.selBegin();
1597 getWord(from, to, PARTIAL_WORD);
1598 cursorRightOneWord(cur);
1601 recordUndoSelection(cur);
1603 pos_type pos = from.pos();
1604 int par = from.pit();
1606 while (par != int(pars_.size()) && (pos != to.pos() || par != to.pit())) {
1608 if (pos == pars_[pit].size()) {
1613 char_type c = pars_[pit].getChar(pos);
1614 if (c != Paragraph::META_INSET) {
1616 case text_lowercase:
1619 case text_capitalization:
1621 action = text_lowercase;
1623 case text_uppercase:
1628 #ifdef WITH_WARNINGS
1631 pars_[pit].setChar(pos, c);
1637 bool LyXText::erase(LCursor & cur)
1639 BOOST_ASSERT(this == cur.text());
1640 bool needsUpdate = false;
1642 if (cur.pos() != cur.lastpos()) {
1643 recordUndo(cur, Undo::DELETE, cur.pit());
1644 setCursorIntern(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1645 needsUpdate = backspace(cur);
1646 // FIXME: change tracking (MG)
1647 if (cur.paragraph().lookupChange(cur.pos()) == Change(Change::DELETED))
1649 } else if (cur.pit() != cur.lastpit()) {
1652 setCursorIntern(cur, cur.pit() + 1, 0, false, false);
1653 if (pars_[cur.pit()].layout() == pars_[scur.pit()].layout()) {
1654 recordUndo(scur, Undo::DELETE, scur.pit());
1655 needsUpdate = backspace(cur);
1656 if (cur.buffer().params().trackChanges) {
1657 // FIXME: Change tracking (MG)
1658 // move forward after the paragraph break is DELETED
1659 Paragraph & par = cur.paragraph();
1660 // FIXME: change tracking (MG)
1661 if (par.lookupChange(par.size()) == Change(Change::DELETED))
1662 setCursorIntern(cur, cur.pit() + 1, 0);
1665 setCursorIntern(scur, scur.pit(), scur.pos(), false, scur.boundary());
1668 needsUpdate = dissolveInset(cur);
1674 bool LyXText::backspacePos0(LCursor & cur)
1676 BOOST_ASSERT(this == cur.text());
1677 bool needsUpdate = false;
1679 Paragraph & par = cur.paragraph();
1680 // is it an empty paragraph?
1681 pos_type lastpos = cur.lastpos();
1682 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1683 // This is an empty paragraph and we delete it just
1684 // by moving the cursor one step
1685 // left and let the DeleteEmptyParagraphMechanism
1686 // handle the actual deletion of the paragraph.
1688 if (cur.pit() != 0) {
1689 // For KeepEmpty layouts we need to get
1690 // rid of the keepEmpty setting first.
1691 // And the only way to do this is to
1692 // reset the layout to something
1693 // else: f.ex. the default layout.
1694 if (par.allowEmpty()) {
1695 Buffer & buf = cur.buffer();
1696 BufferParams const & bparams = buf.params();
1697 par.layout(bparams.getLyXTextClass().defaultLayout());
1706 recordUndo(cur, Undo::DELETE, cur.pit() - 1);
1708 pit_type tmppit = cur.pit();
1709 // We used to do cursorLeftIntern() here, but it is
1710 // not a good idea since it triggers the auto-delete
1711 // mechanism. So we do a cursorLeftIntern()-lite,
1712 // without the dreaded mechanism. (JMarc)
1713 if (cur.pit() != 0) {
1714 // steps into the above paragraph.
1715 setCursorIntern(cur, cur.pit() - 1,
1716 pars_[cur.pit() - 1].size(),
1720 // Pasting is not allowed, if the paragraphs have different
1721 // layout. I think it is a real bug of all other
1722 // word processors to allow it. It confuses the user.
1723 // Correction: Pasting is always allowed with standard-layout
1724 // Correction (Jug 20050717): Remove check about alignment!
1725 Buffer & buf = cur.buffer();
1726 BufferParams const & bufparams = buf.params();
1727 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1728 pit_type const cpit = cur.pit();
1731 && (pars_[cpit].layout() == pars_[tmppit].layout()
1732 || pars_[tmppit].layout() == tclass.defaultLayout()))
1734 mergeParagraph(bufparams, pars_, cpit);
1737 if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
1740 // the counters may have changed
1741 ParIterator par_it(cur);
1742 updateLabels(cur.buffer(), par_it);
1744 setCursor(cur, cur.pit(), cur.pos(), false);
1750 bool LyXText::backspace(LCursor & cur)
1752 BOOST_ASSERT(this == cur.text());
1753 bool needsUpdate = false;
1754 if (cur.pos() == 0) {
1756 return dissolveInset(cur);
1758 // The cursor is at the beginning of a paragraph, so
1759 // the the backspace will collapse two paragraphs into
1762 if (cur.pit() != 0 && cur.buffer().params().trackChanges) {
1763 // FIXME: Change tracking (MG)
1764 // Previous paragraph, mark "carriage return" as
1766 Paragraph & par = pars_[cur.pit() - 1];
1767 // Take care of a just inserted para break:
1768 // FIXME: change tracking (MG)
1769 if (par.lookupChange(par.size()) != Change(Change::INSERTED)) {
1770 // FIXME: change tracking (MG)
1771 par.setChange(par.size(), Change(Change::DELETED));
1772 setCursorIntern(cur, cur.pit() - 1, par.size());
1777 needsUpdate = backspacePos0(cur);
1780 // this is the code for a normal backspace, not pasting
1782 recordUndo(cur, Undo::DELETE);
1783 // We used to do cursorLeftIntern() here, but it is
1784 // not a good idea since it triggers the auto-delete
1785 // mechanism. So we do a cursorLeftIntern()-lite,
1786 // without the dreaded mechanism. (JMarc)
1787 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1788 false, cur.boundary());
1789 // FIXME: change tracking (MG)
1790 cur.paragraph().erase(cur.pos(), cur.buffer().params().trackChanges);
1793 if (cur.pos() == cur.lastpos())
1794 setCurrentFont(cur);
1796 setCursor(cur, cur.pit(), cur.pos(), false, cur.boundary());
1802 bool LyXText::dissolveInset(LCursor & cur) {
1803 BOOST_ASSERT(this == cur.text());
1805 if (isMainText() || cur.inset().nargs() != 1)
1808 recordUndoInset(cur);
1809 cur.selHandle(false);
1811 pos_type spos = cur.pos();
1812 pit_type spit = cur.pit();
1813 ParagraphList plist;
1814 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1815 plist = paragraphs();
1817 // store cursor offset
1821 Buffer & b = cur.buffer();
1822 // FIXME: change tracking (MG)
1823 cur.paragraph().erase(cur.pos(), b.params().trackChanges);
1824 if (!plist.empty()) {
1825 pasteParagraphList(cur, plist, b.params().textclass,
1826 b.errorList("Paste"));
1828 cur.pit() = std::min(cur.lastpit(), spit);
1829 cur.pos() = std::min(cur.lastpos(), spos);
1831 cur.clearSelection();
1837 Row const & LyXText::firstRow() const
1839 return *paragraphs().front().rows().begin();
1843 bool LyXText::redoParagraph(pit_type const pit)
1845 // remove rows of paragraph, keep track of height changes
1846 Paragraph & par = pars_[pit];
1848 // Add bibitem insets if necessary
1849 if (par.layout()->labeltype == LABEL_BIBLIO) {
1850 bool hasbibitem(false);
1851 if (!par.insetlist.empty()
1852 // Insist on it being in pos 0
1853 && par.getChar(0) == Paragraph::META_INSET) {
1854 InsetBase * inset = par.insetlist.begin()->inset;
1855 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
1859 InsetBibitem * inset(new
1860 InsetBibitem(InsetCommandParams("bibitem")));
1861 // FIXME: change tracking (MG)
1862 par.insertInset(0, static_cast<InsetBase *>(inset), Change(Change::INSERTED));
1863 bv()->cursor().posRight();
1868 // FIXME: We should always use getFont(), see documentation of
1869 // noFontChange() in insetbase.h.
1870 LyXFont const bufferfont = bv()->buffer()->params().getFont();
1871 InsetList::iterator ii = par.insetlist.begin();
1872 InsetList::iterator iend = par.insetlist.end();
1873 for (; ii != iend; ++ii) {
1875 int const w = maxwidth_ - leftMargin(pit, ii->pos) - rightMargin(par);
1876 LyXFont const & font = ii->inset->noFontChange() ?
1878 getFont(par, ii->pos);
1879 MetricsInfo mi(bv(), font, w);
1880 ii->inset->metrics(mi, dim);
1883 // rebreak the paragraph
1887 par.setBeginOfBody();
1891 rowBreakPoint(pit, row);
1892 setRowWidth(pit, row);
1893 setHeightOfRow(pit, row);
1894 par.rows().push_back(row);
1895 dim.wid = std::max(dim.wid, row.width());
1896 dim.des += row.height();
1898 } while (z < par.size());
1900 // Make sure that if a par ends in newline, there is one more row
1902 // FIXME this is a dirty trick. Now the _same_ position in the
1903 // paragraph occurs in _two_ different rows, and has two different
1904 // display positions, leading to weird behaviour when moving up/down.
1905 if (z > 0 && par.isNewline(z - 1)) {
1908 setRowWidth(pit, row);
1909 setHeightOfRow(pit, row);
1910 par.rows().push_back(row);
1911 dim.des += row.height();
1914 dim.asc += par.rows()[0].ascent();
1915 dim.des -= par.rows()[0].ascent();
1917 bool const same = dim == par.dim();
1920 //lyxerr << "redoParagraph: " << par.rows().size() << " rows\n";
1926 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1928 //BOOST_ASSERT(mi.base.textwidth);
1929 if (mi.base.textwidth)
1930 maxwidth_ = mi.base.textwidth;
1931 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1932 // << " maxWidth: " << maxwidth_ << "\nfont: " << mi.base.font << endl;
1933 // save the caller's font locally:
1934 font_ = mi.base.font;
1938 for (pit_type pit = 0, n = paragraphs().size(); pit != n; ++pit) {
1940 Paragraph & par = paragraphs()[pit];
1942 if (w < par.width())
1947 dim.asc = pars_[0].ascent();
1948 dim.des = h - dim.asc;
1954 // only used for inset right now. should also be used for main text
1955 void LyXText::draw(PainterInfo & pi, int x, int y) const
1957 paintTextInset(*this, pi, x, y);
1962 // only used for inset right now. should also be used for main text
1963 void LyXText::drawSelection(PainterInfo & pi, int x , int) const
1965 LCursor & cur = pi.base.bv->cursor();
1966 if (!cur.selection())
1968 if (!ptr_cmp(cur.text(), this))
1971 lyxerr << "draw selection at " << x << endl;
1973 DocIterator beg = cur.selectionBegin();
1974 DocIterator end = cur.selectionEnd();
1976 // the selection doesn't touch the visible screen
1977 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
1978 || bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_ABOVE)
1981 Paragraph const & par1 = pars_[beg.pit()];
1982 Paragraph const & par2 = pars_[end.pit()];
1984 Row const & row1 = par1.getRow(beg.pos(), beg.boundary());
1985 Row const & row2 = par2.getRow(end.pos(), end.boundary());
1988 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_ABOVE) {
1993 y1 = bv_funcs::getPos(beg).y_ - row1.ascent();
1994 int const startx = cursorX(beg.top(), begin.boundary());
1995 x1 = isRTL(par1) ? startx : 0;
1996 x2 = isRTL(par1) ? 0 + dim_.wid : startx;
2000 if (bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_BELOW) {
2001 y2 = pi.base.bv->workHeight();
2005 y2 = bv_funcs::getPos(end).y_ + row2.descent();
2006 int const endx = cursorX(end.top(), end.boundary());
2007 X1 = isRTL(par2) ? 0 : endx;
2008 X2 = isRTL(par2) ? endx : 0 + dim_.wid;
2011 lyxerr << " y1: " << y1 << " y2: " << y2
2012 << " xo: " << xo_ << " wid: " << dim_.wid
2015 // paint big rectangle in one go
2016 pi.pain.fillRectangle(x, y1, dim_.wid, y2 - y1, LColor::selection);
2018 // reset background at begin of first selected line
2019 pi.pain.fillRectangle(x + x1, y1, x2 - x1, row1.height(),
2020 LColor::background);
2022 // reset background at end of last selected line
2023 pi.pain.fillRectangle(x + X1, y2 - row2.height(),
2024 X2 - X1, row2.height(), LColor::background);
2029 void LyXText::drawSelection(PainterInfo & pi, int x, int) const
2031 LCursor & cur = pi.base.bv->cursor();
2032 if (!cur.selection())
2034 if (!ptr_cmp(cur.text(), this))
2037 lyxerr[Debug::DEBUG]
2038 << BOOST_CURRENT_FUNCTION
2039 << "draw selection at " << x
2042 DocIterator beg = cur.selectionBegin();
2043 DocIterator end = cur.selectionEnd();
2045 BufferView * bv = pi.base.bv;
2047 // the selection doesn't touch the visible screen
2048 if (bv_funcs::status(bv, beg) == bv_funcs::CUR_BELOW
2049 || bv_funcs::status(bv, end) == bv_funcs::CUR_ABOVE)
2052 Paragraph const & par1 = pars_[beg.pit()];
2053 Paragraph const & par2 = pars_[end.pit()];
2055 bool const above = (bv_funcs::status(bv, beg)
2056 == bv_funcs::CUR_ABOVE);
2057 bool const below = (bv_funcs::status(bv, end)
2058 == bv_funcs::CUR_BELOW);
2066 Row const & row1 = par1.getRow(beg.pos(), beg.boundary());
2067 y1 = bv_funcs::getPos(*bv, beg, beg.boundary()).y_ - row1.ascent();
2068 y2 = y1 + row1.height();
2069 int const startx = cursorX(beg.top(), beg.boundary());
2070 x1 = !isRTL(par1) ? startx : 0;
2071 x2 = !isRTL(par1) ? 0 + dim_.wid : startx;
2076 Y1 = bv->workHeight();
2077 Y2 = bv->workHeight();
2081 Row const & row2 = par2.getRow(end.pos(), end.boundary());
2082 Y1 = bv_funcs::getPos(*bv, end, end.boundary()).y_ - row2.ascent();
2083 Y2 = Y1 + row2.height();
2084 int const endx = cursorX(end.top(), end.boundary());
2085 X1 = !isRTL(par2) ? 0 : endx;
2086 X2 = !isRTL(par2) ? endx : 0 + dim_.wid;
2089 if (!above && !below && &par1.getRow(beg.pos(), beg.boundary())
2090 == &par2.getRow(end.pos(), end.boundary()))
2092 // paint only one rectangle
2093 int const b( !isRTL(par1) ? x + x1 : x + X1 );
2094 int const w( !isRTL(par1) ? X2 - x1 : x2 - X1 );
2095 pi.pain.fillRectangle(b, y1, w, y2 - y1, LColor::selection);
2099 lyxerr[Debug::DEBUG] << " y1: " << y1 << " y2: " << y2
2100 << "X1:" << X1 << " x2: " << X2 << " wid: " << dim_.wid
2103 // paint upper rectangle
2104 pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
2106 // paint bottom rectangle
2107 pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
2109 // paint center rectangle
2110 pi.pain.fillRectangle(x, y2, dim_.wid,
2111 Y1 - y2, LColor::selection);
2115 bool LyXText::isLastRow(pit_type pit, Row const & row) const
2117 return row.endpos() >= pars_[pit].size()
2118 && pit + 1 == pit_type(paragraphs().size());
2122 bool LyXText::isFirstRow(pit_type pit, Row const & row) const
2124 return row.pos() == 0 && pit == 0;
2128 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
2129 word_location const loc)
2131 Paragraph const & from_par = pars_[from.pit()];
2133 case WHOLE_WORD_STRICT:
2134 if (from.pos() == 0 || from.pos() == from_par.size()
2135 || !from_par.isLetter(from.pos())
2136 || !from_par.isLetter(from.pos() - 1)) {
2140 // no break here, we go to the next
2143 // If we are already at the beginning of a word, do nothing
2144 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
2146 // no break here, we go to the next
2149 // always move the cursor to the beginning of previous word
2150 while (from.pos() && from_par.isLetter(from.pos() - 1))
2154 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
2158 // no need to move the 'from' cursor
2162 Paragraph & to_par = pars_[to.pit()];
2163 while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
2168 void LyXText::write(Buffer const & buf, std::ostream & os) const
2170 ParagraphList::const_iterator pit = paragraphs().begin();
2171 ParagraphList::const_iterator end = paragraphs().end();
2173 for (; pit != end; ++pit)
2174 pit->write(buf, os, buf.params(), dth);
2178 bool LyXText::read(Buffer const & buf, LyXLex & lex, ErrorList & errorList)
2180 depth_type depth = 0;
2182 while (lex.isOK()) {
2184 string const token = lex.getString();
2189 if (token == "\\end_inset")
2192 if (token == "\\end_body")
2195 if (token == "\\begin_body")
2198 if (token == "\\end_document")
2201 if (token == "\\begin_layout") {
2202 lex.pushToken(token);
2205 par.params().depth(depth);
2206 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
2207 pars_.push_back(par);
2209 // FIXME: goddamn InsetTabular makes us pass a Buffer
2211 lyx::readParagraph(buf, pars_.back(), lex, errorList);
2213 } else if (token == "\\begin_deeper") {
2215 } else if (token == "\\end_deeper") {
2217 lex.printError("\\end_deeper: " "depth is already null");
2222 lyxerr << "Handling unknown body token: `"
2223 << token << '\'' << endl;
2230 int LyXText::ascent() const
2236 int LyXText::descent() const
2242 int LyXText::cursorX(CursorSlice const & sl, bool boundary) const
2244 pit_type const pit = sl.pit();
2245 Paragraph const & par = pars_[pit];
2246 if (par.rows().empty())
2249 pos_type ppos = sl.pos();
2250 // Correct position in front of big insets
2251 bool const boundary_correction = ppos != 0 && boundary;
2252 if (boundary_correction)
2255 Row const & row = par.getRow(sl.pos(), boundary);
2257 pos_type cursor_vpos = 0;
2259 RowMetrics const m = computeRowMetrics(pit, row);
2262 pos_type const row_pos = row.pos();
2263 pos_type const end = row.endpos();
2266 cursor_vpos = row_pos;
2267 else if (ppos >= end)
2268 cursor_vpos = isRTL(par) ? row_pos : end;
2269 else if (ppos > row_pos && ppos >= end)
2270 // Place cursor after char at (logical) position pos - 1
2271 cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
2272 ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
2274 // Place cursor before char at (logical) position ppos
2275 cursor_vpos = (bidi.level(ppos) % 2 == 0)
2276 ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
2278 pos_type body_pos = par.beginOfBody();
2280 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
2283 // Use font span to speed things up, see below
2286 FontMetrics const & labelfm = theFontMetrics(getLabelFont(par));
2288 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
2289 pos_type pos = bidi.vis2log(vpos);
2290 if (body_pos > 0 && pos == body_pos - 1) {
2292 docstring const lsep = from_utf8(par.layout()->labelsep);
2293 x += m.label_hfill + labelfm.width(lsep);
2294 if (par.isLineSeparator(body_pos - 1))
2295 x -= singleWidth(par, body_pos - 1);
2298 // Use font span to speed things up, see above
2299 if (pos < font_span.first || pos > font_span.last) {
2300 font_span = par.fontSpan(pos);
2301 font = getFont(par, pos);
2304 x += singleWidth(par, pos, par.getChar(pos), font);
2306 if (hfillExpansion(par, row, pos))
2307 x += (pos >= body_pos) ? m.hfill : m.label_hfill;
2308 else if (par.isSeparator(pos) && pos >= body_pos)
2312 // see correction above
2313 if (boundary_correction)
2314 if (getFont(par, ppos).isVisibleRightToLeft())
2315 x -= singleWidth(par, ppos);
2317 x += singleWidth(par, ppos);
2319 // Make sure inside an inset we always count from the left
2320 // edge (bidi!) -- MV
2321 if (sl.pos() < par.size()) {
2322 font = getFont(par, sl.pos());
2323 if (!boundary && font.isVisibleRightToLeft()
2324 && par.isInset(sl.pos()))
2325 x -= par.getInset(sl.pos())->width();
2331 int LyXText::cursorY(CursorSlice const & sl, bool boundary) const
2333 //lyxerr << "LyXText::cursorY: boundary: " << boundary << std::endl;
2334 Paragraph const & par = getPar(sl.pit());
2335 if (par.rows().empty())
2339 h -= pars_[0].rows()[0].ascent();
2340 for (pit_type pit = 0; pit < sl.pit(); ++pit)
2341 h += pars_[pit].height();
2343 if (pos && boundary)
2345 size_t const rend = par.pos2row(pos);
2346 for (size_t rit = 0; rit != rend; ++rit)
2347 h += par.rows()[rit].height();
2348 h += par.rows()[rend].ascent();
2353 // Returns the current font and depth as a message.
2354 string LyXText::currentState(LCursor & cur)
2356 BOOST_ASSERT(this == cur.text());
2357 Buffer & buf = cur.buffer();
2358 Paragraph const & par = cur.paragraph();
2359 std::ostringstream os;
2361 // FIXME: change tracking (MG)
2362 bool const show_change = par.lookupChange(cur.pos()) != Change(Change::UNCHANGED);
2364 if (buf.params().trackChanges)
2368 Change change = par.lookupChange(cur.pos());
2369 Author const & a = buf.params().authors().get(change.author);
2370 os << to_utf8(_("Change: ")) << a.name();
2371 if (!a.email().empty())
2372 os << " (" << a.email() << ")";
2373 if (change.changetime)
2374 os << to_utf8(_(" at ")) << ctime(&change.changetime);
2378 // I think we should only show changes from the default
2380 // No, from the document font (MV)
2381 LyXFont font = real_current_font;
2382 font.reduce(buf.params().getFont());
2384 // avoid to_utf8(_(...)) re-entrance problem
2385 string const s = font.stateText(&buf.params());
2386 os << to_utf8(bformat(_("Font: %1$s"), from_utf8(s)));
2388 // os << to_utf8(bformat(_("Font: %1$s"), font.stateText(&buf.params)));
2390 // The paragraph depth
2391 int depth = cur.paragraph().getDepth();
2393 os << to_utf8(bformat(_(", Depth: %1$d"), depth));
2395 // The paragraph spacing, but only if different from
2397 Spacing const & spacing = par.params().spacing();
2398 if (!spacing.isDefault()) {
2399 os << to_utf8(_(", Spacing: "));
2400 switch (spacing.getSpace()) {
2401 case Spacing::Single:
2402 os << to_utf8(_("Single"));
2404 case Spacing::Onehalf:
2405 os << to_utf8(_("OneHalf"));
2407 case Spacing::Double:
2408 os << to_utf8(_("Double"));
2410 case Spacing::Other:
2411 os << to_utf8(_("Other (")) << spacing.getValueAsString() << ')';
2413 case Spacing::Default:
2414 // should never happen, do nothing
2419 #ifdef DEVEL_VERSION
2420 os << to_utf8(_(", Inset: ")) << &cur.inset();
2421 os << to_utf8(_(", Paragraph: ")) << cur.pit();
2422 os << to_utf8(_(", Id: ")) << par.id();
2423 os << to_utf8(_(", Position: ")) << cur.pos();
2424 os << to_utf8(_(", Boundary: ")) << cur.boundary();
2425 // Row & row = cur.textRow();
2426 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2432 string LyXText::getPossibleLabel(LCursor & cur) const
2434 pit_type pit = cur.pit();
2436 LyXLayout_ptr layout = pars_[pit].layout();
2438 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2439 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
2440 if (layout2->latextype != LATEX_PARAGRAPH) {
2446 string name = layout->latexname();
2448 // for captions, we want the abbreviation of the float type
2449 if (layout->labeltype == LABEL_SENSITIVE) {
2450 // Search for the first float or wrap inset in the iterator
2451 size_t i = cur.depth();
2454 InsetBase * const in = &cur[i].inset();
2455 if (in->lyxCode() == InsetBase::FLOAT_CODE
2456 || in->lyxCode() == InsetBase::WRAP_CODE) {
2457 name = in->getInsetName();
2463 string text = name.substr(0, 3);
2464 if (name == "theorem")
2465 text = "thm"; // Create a correct prefix for prettyref
2468 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2472 string par_text = to_utf8(pars_[pit].asString(cur.buffer(), false));
2473 for (int i = 0; i < lyxrc.label_init_length; ++i) {
2474 if (par_text.empty())
2477 par_text = split(par_text, head, ' ');
2478 // Is it legal to use spaces in labels ?
2488 //pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2492 // Paragraph const & par = pars_[pit];
2493 // Row const & r = par.rows()[row];
2494 // int pos = r.pos();
2495 // for (; currx < x && pos < r.endpos(); ++pos) {
2497 // currx += singleWidth(par, pos);
2499 // if (abs(lastx - x) < abs(currx - x) && pos != r.pos())
2505 pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2507 BOOST_ASSERT(row < int(pars_[pit].rows().size()));
2509 Row const & r = pars_[pit].rows()[row];
2510 return r.pos() + getColumnNearX(pit, r, x, bound);
2514 //int LyXText::pos2x(pit_type pit, pos_type pos) const
2516 // Paragraph const & par = pars_[pit];
2517 // Row const & r = par.rows()[row];
2523 // x,y are screen coordinates
2524 // sets cursor only within this LyXText
2525 bool LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
2527 BOOST_ASSERT(this == cur.text());
2528 pit_type pit = getPitNearY(y);
2529 int yy = cur.bv().coordCache().get(this, pit).y_ - pars_[pit].ascent();
2530 lyxerr[Debug::DEBUG]
2531 << BOOST_CURRENT_FUNCTION
2535 << " yy: " << yy << endl;
2537 Paragraph const & par = pars_[pit];
2539 BOOST_ASSERT(par.rows().size());
2540 for (; r < int(par.rows().size()) - 1; ++r) {
2541 Row const & row = par.rows()[r];
2542 if (int(yy + row.height()) > y)
2547 Row const & row = par.rows()[r];
2549 lyxerr[Debug::DEBUG]
2550 << BOOST_CURRENT_FUNCTION
2552 << " from pos: " << row.pos()
2557 pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
2559 lyxerr[Debug::DEBUG]
2560 << BOOST_CURRENT_FUNCTION
2561 << ": setting cursor pit: " << pit
2565 return setCursor(cur, pit, pos, true, bound);