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 "coordcache.h"
27 #include "CutAndPaste.h"
29 #include "dispatchresult.h"
31 #include "errorlist.h"
32 #include "funcrequest.h"
34 #include "FontIterator.h"
38 #include "lyxlength.h"
42 #include "lyxrow_funcs.h"
43 #include "metricsinfo.h"
44 #include "paragraph.h"
45 #include "paragraph_funcs.h"
46 #include "ParagraphParameters.h"
47 #include "rowpainter.h"
50 #include "WordLangTuple.h"
52 #include "frontends/font_metrics.h"
53 #include "frontends/LyXView.h"
54 #include "frontends/Painter.h"
56 #include "insets/insettext.h"
57 #include "insets/insetbibitem.h"
58 #include "insets/insethfill.h"
59 #include "insets/insetlatexaccent.h"
60 #include "insets/insetline.h"
61 #include "insets/insetnewline.h"
62 #include "insets/insetpagebreak.h"
63 #include "insets/insetoptarg.h"
64 #include "insets/insetspace.h"
65 #include "insets/insetspecialchar.h"
66 #include "insets/insettabular.h"
68 #include "support/lstrings.h"
69 #include "support/textutils.h"
70 #include "support/tostr.h"
76 using lyx::word_location;
78 using lyx::support::bformat;
79 using lyx::support::contains;
80 using lyx::support::lowercase;
81 using lyx::support::split;
82 using lyx::support::uppercase;
84 using lyx::cap::cutSelection;
97 int numberOfSeparators(Paragraph const & par, Row const & row)
99 pos_type const first = max(row.pos(), par.beginOfBody());
100 pos_type const last = row.endpos() - 1;
102 for (pos_type p = first; p < last; ++p) {
103 if (par.isSeparator(p))
110 int numberOfLabelHfills(Paragraph const & par, Row const & row)
112 pos_type last = row.endpos() - 1;
113 pos_type first = row.pos();
115 // hfill *DO* count at the beginning of paragraphs!
117 while (first < last && par.isHfill(first))
121 last = min(last, par.beginOfBody());
123 for (pos_type p = first; p < last; ++p) {
131 int numberOfHfills(Paragraph const & par, Row const & row)
133 pos_type const last = row.endpos() - 1;
134 pos_type first = row.pos();
136 // hfill *DO* count at the beginning of paragraphs!
138 while (first < last && par.isHfill(first))
142 first = max(first, par.beginOfBody());
145 for (pos_type p = first; p < last; ++p) {
153 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
154 string const & token, LyXFont & font)
156 static Change change;
158 BufferParams const & bp = buf.params();
160 if (token[0] != '\\') {
161 string::const_iterator cit = token.begin();
162 for (; cit != token.end(); ++cit) {
163 par.insertChar(par.size(), (*cit), font, change);
165 } else if (token == "\\begin_layout") {
167 string layoutname = lex.getString();
169 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
172 LyXTextClass const & tclass = bp.getLyXTextClass();
174 if (layoutname.empty()) {
175 layoutname = tclass.defaultLayoutName();
178 bool hasLayout = tclass.hasLayout(layoutname);
181 buf.error(ErrorItem(_("Unknown layout"),
182 bformat(_("Layout '%1$s' does not exists in textclass '%2$s'\nTrying to use the default instead.\n"),
183 layoutname, tclass.name()), par.id(), 0, par.size()));
184 layoutname = tclass.defaultLayoutName();
187 par.layout(bp.getLyXTextClass()[layoutname]);
189 // Test whether the layout is obsolete.
190 LyXLayout_ptr const & layout = par.layout();
191 if (!layout->obsoleted_by().empty())
192 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
194 par.params().read(lex);
196 } else if (token == "\\end_layout") {
197 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
198 << "Missing \\begin_layout?.\n";
199 } else if (token == "\\end_inset") {
200 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
201 << "Missing \\begin_inset?.\n";
202 } else if (token == "\\begin_inset") {
203 InsetBase * inset = readInset(lex, buf);
205 par.insertInset(par.size(), inset, font, change);
208 string line = lex.getString();
209 buf.error(ErrorItem(_("Unknown Inset"), line,
210 par.id(), 0, par.size()));
212 } else if (token == "\\family") {
214 font.setLyXFamily(lex.getString());
215 } else if (token == "\\series") {
217 font.setLyXSeries(lex.getString());
218 } else if (token == "\\shape") {
220 font.setLyXShape(lex.getString());
221 } else if (token == "\\size") {
223 font.setLyXSize(lex.getString());
224 } else if (token == "\\lang") {
226 string const tok = lex.getString();
227 Language const * lang = languages.getLanguage(tok);
229 font.setLanguage(lang);
231 font.setLanguage(bp.language);
232 lex.printError("Unknown language `$$Token'");
234 } else if (token == "\\numeric") {
236 font.setNumber(font.setLyXMisc(lex.getString()));
237 } else if (token == "\\emph") {
239 font.setEmph(font.setLyXMisc(lex.getString()));
240 } else if (token == "\\bar") {
242 string const tok = lex.getString();
245 font.setUnderbar(LyXFont::ON);
246 else if (tok == "no")
247 font.setUnderbar(LyXFont::OFF);
248 else if (tok == "default")
249 font.setUnderbar(LyXFont::INHERIT);
251 lex.printError("Unknown bar font flag "
253 } else if (token == "\\noun") {
255 font.setNoun(font.setLyXMisc(lex.getString()));
256 } else if (token == "\\color") {
258 font.setLyXColor(lex.getString());
259 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
261 // Insets don't make sense in a free-spacing context! ---Kayvan
262 if (par.isFreeSpacing()) {
263 if (token == "\\InsetSpace")
264 par.insertChar(par.size(), ' ', font, change);
265 else if (lex.isOK()) {
267 string const next_token = lex.getString();
268 if (next_token == "\\-")
269 par.insertChar(par.size(), '-', font, change);
271 lex.printError("Token `$$Token' "
273 "paragraph layout!");
277 auto_ptr<InsetBase> inset;
278 if (token == "\\SpecialChar" )
279 inset.reset(new InsetSpecialChar);
281 inset.reset(new InsetSpace);
282 inset->read(buf, lex);
283 par.insertInset(par.size(), inset.release(),
286 } else if (token == "\\i") {
287 auto_ptr<InsetBase> inset(new InsetLatexAccent);
288 inset->read(buf, lex);
289 par.insertInset(par.size(), inset.release(), font, change);
290 } else if (token == "\\backslash") {
291 par.insertChar(par.size(), '\\', font, change);
292 } else if (token == "\\newline") {
293 auto_ptr<InsetBase> inset(new InsetNewline);
294 inset->read(buf, lex);
295 par.insertInset(par.size(), inset.release(), font, change);
296 } else if (token == "\\LyXTable") {
297 auto_ptr<InsetBase> inset(new InsetTabular(buf));
298 inset->read(buf, lex);
299 par.insertInset(par.size(), inset.release(), font, change);
300 } else if (token == "\\bibitem") {
301 InsetCommandParams p("bibitem", "dummy");
302 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
303 inset->read(buf, lex);
304 par.insertInset(par.size(), inset.release(), font, change);
305 } else if (token == "\\hfill") {
306 par.insertInset(par.size(), new InsetHFill, font, change);
307 } else if (token == "\\lyxline") {
308 par.insertInset(par.size(), new InsetLine, font, change);
309 } else if (token == "\\newpage") {
310 par.insertInset(par.size(), new InsetPagebreak, font, change);
311 } else if (token == "\\change_unchanged") {
312 // Hack ! Needed for empty paragraphs :/
313 // FIXME: is it still ??
316 change = Change(Change::UNCHANGED);
317 } else if (token == "\\change_inserted") {
319 std::istringstream is(lex.getString());
323 change = Change(Change::INSERTED, bp.author_map[aid], ct);
324 } else if (token == "\\change_deleted") {
326 std::istringstream is(lex.getString());
330 change = Change(Change::DELETED, bp.author_map[aid], ct);
333 buf.error(ErrorItem(_("Unknown token"),
334 bformat(_("Unknown token: %1$s %2$s\n"), token, lex.getString()),
335 par.id(), 0, par.size()));
340 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex)
343 string token = lex.getString();
348 readParToken(buf, par, lex, token, font);
351 token = lex.getString();
356 if (token == "\\end_layout") {
357 //Ok, paragraph finished
361 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
362 << token << '\'' << endl;
363 if (token == "\\begin_layout" || token == "\\end_document"
364 || token == "\\end_inset" || token == "\\begin_deeper"
365 || token == "\\end_deeper") {
366 lex.pushToken(token);
367 lyxerr << "Paragraph ended in line "
368 << lex.getLineNo() << "\n"
369 << "Missing \\end_layout.\n";
379 BufferView * LyXText::bv()
381 BOOST_ASSERT(bv_owner != 0);
386 BufferView * LyXText::bv() const
388 BOOST_ASSERT(bv_owner != 0);
393 double LyXText::spacing(Paragraph const & par) const
395 if (par.params().spacing().isDefault())
396 return bv()->buffer()->params().spacing().getValue();
397 return par.params().spacing().getValue();
401 int LyXText::width() const
407 int LyXText::height() const
409 return dim_.height();
413 int LyXText::singleWidth(Paragraph const & par, pos_type pos) const
415 return singleWidth(par, pos, par.getChar(pos), getFont(par, pos));
419 int LyXText::singleWidth(Paragraph const & par,
420 pos_type pos, char c, LyXFont const & font) const
422 BOOST_ASSERT(pos < par.size());
424 // The most common case is handled first (Asger)
425 if (IsPrintable(c)) {
426 if (font.language()->RightToLeft()) {
427 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
428 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
429 && font.language()->lang() == "arabic") {
430 if (Encodings::IsComposeChar_arabic(c))
432 c = par.transformChar(c, pos);
433 } else if (font.language()->lang() == "hebrew" &&
434 Encodings::IsComposeChar_hebrew(c))
437 return font_metrics::width(c, font);
440 if (c == Paragraph::META_INSET)
441 return par.getInset(pos)->width();
443 if (IsSeparatorChar(c))
445 return font_metrics::width(c, font);
449 int LyXText::leftMargin(pit_type pit) const
451 BOOST_ASSERT(pit >= 0);
452 BOOST_ASSERT(pit < int(pars_.size()));
453 return leftMargin(pit, pars_[pit].size());
457 int LyXText::leftMargin(pit_type const pit, pos_type const pos) const
459 BOOST_ASSERT(pit >= 0);
460 BOOST_ASSERT(pit < int(pars_.size()));
461 Paragraph const & par = pars_[pit];
462 BOOST_ASSERT(pos >= 0);
463 BOOST_ASSERT(pos <= par.size());
464 //lyxerr << "LyXText::leftMargin: pit: " << pit << " pos: " << pos << endl;
465 LyXTextClass const & tclass =
466 bv()->buffer()->params().getLyXTextClass();
467 LyXLayout_ptr const & layout = par.layout();
469 string parindent = layout->parindent;
474 l_margin += changebarMargin();
476 l_margin += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
478 if (par.getDepth() != 0) {
479 // find the next level paragraph
480 pit_type newpar = outerHook(pit, pars_);
481 if (newpar != pit_type(pars_.size())) {
482 if (pars_[newpar].layout()->isEnvironment()) {
483 l_margin = leftMargin(newpar);
485 if (par.layout() == tclass.defaultLayout()) {
486 if (pars_[newpar].params().noindent())
489 parindent = pars_[newpar].layout()->parindent;
494 LyXFont const labelfont = getLabelFont(par);
495 switch (layout->margintype) {
497 if (!layout->leftmargin.empty())
498 l_margin += font_metrics::signedWidth(layout->leftmargin,
499 tclass.defaultfont());
500 if (!par.getLabelstring().empty()) {
501 l_margin += font_metrics::signedWidth(layout->labelindent,
503 l_margin += font_metrics::width(par.getLabelstring(),
505 l_margin += font_metrics::width(layout->labelsep, labelfont);
510 l_margin += font_metrics::signedWidth(layout->labelindent, labelfont);
511 // The width of an empty par, even with manual label, should be 0
512 if (!par.empty() && pos >= par.beginOfBody()) {
513 if (!par.getLabelWidthString().empty()) {
514 l_margin += font_metrics::width(par.getLabelWidthString(),
516 l_margin += font_metrics::width(layout->labelsep, labelfont);
522 l_margin += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
523 / (par.getDepth() + 4);
526 case MARGIN_FIRST_DYNAMIC:
527 if (layout->labeltype == LABEL_MANUAL) {
528 if (pos >= par.beginOfBody()) {
529 l_margin += font_metrics::signedWidth(layout->leftmargin,
532 l_margin += font_metrics::signedWidth(layout->labelindent,
536 // Special case to fix problems with
538 || (layout->labeltype == LABEL_STATIC
539 && layout->latextype == LATEX_ENVIRONMENT
540 && !isFirstInSequence(pit, pars_))) {
541 l_margin += font_metrics::signedWidth(layout->leftmargin,
543 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
544 && layout->labeltype != LABEL_BIBLIO
545 && layout->labeltype !=
546 LABEL_CENTERED_TOP_ENVIRONMENT) {
547 l_margin += font_metrics::signedWidth(layout->labelindent,
549 l_margin += font_metrics::width(layout->labelsep, labelfont);
550 l_margin += font_metrics::width(par.getLabelstring(),
555 case MARGIN_RIGHT_ADDRESS_BOX: {
557 // ok, a terrible hack. The left margin depends on the widest
558 // row in this paragraph.
559 RowList::iterator rit = par.rows().begin();
560 RowList::iterator end = par.rows().end();
562 #warning This is wrong.
564 int minfill = maxwidth_;
565 for ( ; rit != end; ++rit)
566 if (rit->fill() < minfill)
567 minfill = rit->fill();
568 l_margin += font_metrics::signedWidth(layout->leftmargin,
569 tclass.defaultfont());
572 // also wrong, but much shorter.
573 l_margin += maxwidth_ / 2;
578 if (!par.params().leftIndent().zero())
579 l_margin += par.params().leftIndent().inPixels(maxwidth_);
583 if (par.params().align() == LYX_ALIGN_LAYOUT)
584 align = layout->align;
586 align = par.params().align();
588 // set the correct parindent
590 && (layout->labeltype == LABEL_NO_LABEL
591 || layout->labeltype == LABEL_TOP_ENVIRONMENT
592 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
593 || (layout->labeltype == LABEL_STATIC
594 && layout->latextype == LATEX_ENVIRONMENT
595 && !isFirstInSequence(pit, pars_)))
596 && align == LYX_ALIGN_BLOCK
597 && !par.params().noindent()
598 // in charstyles, tabulars and ert paragraphs are never indented!
599 && (par.ownerCode() != InsetBase::TEXT_CODE
600 && par.ownerCode() != InsetBase::ERT_CODE
601 && par.ownerCode() != InsetBase::CHARSTYLE_CODE)
602 && (par.layout() != tclass.defaultLayout()
603 || bv()->buffer()->params().paragraph_separation ==
604 BufferParams::PARSEP_INDENT))
606 l_margin += font_metrics::signedWidth(parindent, tclass.defaultfont());
613 int LyXText::rightMargin(Paragraph const & par) const
615 // We do not want rightmargins on inner texts.
616 if (bv()->text() != this)
619 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
622 + font_metrics::signedWidth(tclass.rightmargin(),
623 tclass.defaultfont())
624 + font_metrics::signedWidth(par.layout()->rightmargin,
625 tclass.defaultfont())
626 * 4 / (par.getDepth() + 4);
632 int LyXText::labelEnd(pit_type const pit) const
634 // labelEnd is only needed if the layout fills a flushleft label.
635 if (pars_[pit].layout()->margintype != MARGIN_MANUAL)
637 // return the beginning of the body
638 return leftMargin(pit);
644 // this needs special handling - only newlines count as a break point
645 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
647 pos_type const end = par.size();
650 if (par.isNewline(i))
659 void LyXText::rowBreakPoint(pit_type const pit, Row & row) const
661 Paragraph const & par = pars_[pit];
662 pos_type const end = par.size();
663 pos_type const pos = row.pos();
669 // maximum pixel width of a row
670 int width = maxwidth_ - rightMargin(par); // - leftMargin(pit, row);
676 LyXLayout_ptr const & layout = par.layout();
678 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
679 row.endpos(addressBreakPoint(pos, par));
683 pos_type const body_pos = par.beginOfBody();
686 // Now we iterate through until we reach the right margin
687 // or the end of the par, then choose the possible break
690 int const left = leftMargin(pit, pos);
693 // pixel width since last breakpoint
696 FontIterator fi = FontIterator(*this, par, pos);
697 pos_type point = end;
699 for ( ; i < end; ++i, ++fi) {
700 char const c = par.getChar(i);
703 int thiswidth = singleWidth(par, i, c, *fi);
705 // add the auto-hfill from label end to the body
706 if (body_pos && i == body_pos) {
707 int add = font_metrics::width(layout->labelsep, getLabelFont(par));
708 if (par.isLineSeparator(i - 1))
709 add -= singleWidth(par, i - 1);
711 add = std::max(add, labelEnd(pit) - x);
716 chunkwidth += thiswidth;
719 // break before a character that will fall off
720 // the right of the row
722 // if no break before, break here
723 if (point == end || chunkwidth >= width - left) {
730 // exit on last registered breakpoint:
734 if (par.isNewline(i)) {
740 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
745 if (par.isInset(i) && par.getInset(i)->display()) {
751 if (!par.isInset(i) || par.getInset(i)->isChar()) {
752 // some insets are line separators too
753 if (par.isLineSeparator(i)) {
754 // register breakpoint:
761 // maybe found one, but the par is short enough.
762 if (i == end && x < width)
765 // manual labels cannot be broken in LaTeX. But we
766 // want to make our on-screen rendering of footnotes
768 if (body_pos && point < body_pos)
775 void LyXText::setRowWidth(pit_type const pit, Row & row) const
777 // get the pure distance
778 pos_type const end = row.endpos();
780 Paragraph const & par = pars_[pit];
781 string labelsep = par.layout()->labelsep;
782 int w = leftMargin(pit, row.pos());
784 pos_type const body_pos = par.beginOfBody();
785 pos_type i = row.pos();
788 FontIterator fi = FontIterator(*this, par, i);
789 for ( ; i < end; ++i, ++fi) {
790 if (body_pos > 0 && i == body_pos) {
791 w += font_metrics::width(labelsep, getLabelFont(par));
792 if (par.isLineSeparator(i - 1))
793 w -= singleWidth(par, i - 1);
794 w = max(w, labelEnd(pit));
796 char const c = par.getChar(i);
797 w += singleWidth(par, i, c, *fi);
801 if (body_pos > 0 && body_pos >= end) {
802 w += font_metrics::width(labelsep, getLabelFont(par));
803 if (end > 0 && par.isLineSeparator(end - 1))
804 w -= singleWidth(par, end - 1);
805 w = max(w, labelEnd(pit));
808 row.width(w + rightMargin(par));
812 // returns the minimum space a manual label needs on the screen in pixel
813 int LyXText::labelFill(Paragraph const & par, Row const & row) const
815 pos_type last = par.beginOfBody();
817 BOOST_ASSERT(last > 0);
819 // -1 because a label ends with a space that is in the label
822 // a separator at this end does not count
823 if (par.isLineSeparator(last))
827 for (pos_type i = row.pos(); i <= last; ++i)
828 w += singleWidth(par, i);
830 string const & label = par.params().labelWidthString();
834 return max(0, font_metrics::width(label, getLabelFont(par)) - w);
838 LColor_color LyXText::backgroundColor() const
840 return LColor_color(LColor::color(background_color_));
844 void LyXText::setHeightOfRow(pit_type const pit, Row & row)
846 Paragraph const & par = pars_[pit];
847 // get the maximum ascent and the maximum descent
848 double layoutasc = 0;
849 double layoutdesc = 0;
850 double const dh = defaultRowHeight();
852 // ok, let us initialize the maxasc and maxdesc value.
853 // Only the fontsize count. The other properties
854 // are taken from the layoutfont. Nicer on the screen :)
855 LyXLayout_ptr const & layout = par.layout();
857 // as max get the first character of this row then it can
858 // increase but not decrease the height. Just some point to
859 // start with so we don't have to do the assignment below too
861 LyXFont font = getFont(par, row.pos());
862 LyXFont::FONT_SIZE const tmpsize = font.size();
863 font = getLayoutFont(pit);
864 LyXFont::FONT_SIZE const size = font.size();
865 font.setSize(tmpsize);
867 LyXFont labelfont = getLabelFont(par);
869 // these are minimum values
870 double const spacing_val = layout->spacing.getValue() * spacing(par);
871 //lyxerr << "spacing_val = " << spacing_val << endl;
872 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
873 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
875 // insets may be taller
876 InsetList::const_iterator ii = par.insetlist.begin();
877 InsetList::const_iterator iend = par.insetlist.end();
878 for ( ; ii != iend; ++ii) {
879 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
880 maxasc = max(maxasc, ii->inset->ascent());
881 maxdesc = max(maxdesc, ii->inset->descent());
885 // Check if any custom fonts are larger (Asger)
886 // This is not completely correct, but we can live with the small,
887 // cosmetic error for now.
889 pos_type const pos_end = row.endpos();
891 LyXFont::FONT_SIZE maxsize =
892 par.highestFontInRange(row.pos(), pos_end, size);
893 if (maxsize > font.size()) {
894 font.setSize(maxsize);
895 maxasc = max(maxasc, font_metrics::maxAscent(font));
896 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
899 // This is nicer with box insets:
906 if (row.pos() == 0) {
907 BufferParams const & bufparams = bv()->buffer()->params();
908 // some parksips VERY EASY IMPLEMENTATION
909 if (bv()->buffer()->params().paragraph_separation
910 == BufferParams::PARSEP_SKIP
912 && ((layout->isParagraph() && par.getDepth() == 0)
913 || (pars_[pit - 1].layout()->isParagraph()
914 && pars_[pit - 1].getDepth() == 0)))
916 maxasc += bufparams.getDefSkip().inPixels(*bv());
919 if (pars_[pit].params().startOfAppendix())
920 maxasc += int(3 * dh);
922 // This is special code for the chapter, since the label of this
923 // layout is printed in an extra row
924 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
925 labeladdon = int(font_metrics::maxHeight(labelfont)
926 * layout->spacing.getValue()
930 // special code for the top label
931 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
932 || layout->labeltype == LABEL_BIBLIO
933 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
934 && isFirstInSequence(pit, paragraphs())
935 && !par.getLabelstring().empty())
938 font_metrics::maxHeight(labelfont)
939 * layout->spacing.getValue()
941 + (layout->topsep + layout->labelbottomsep) * dh);
944 // Add the layout spaces, for example before and after
945 // a section, or between the items of a itemize or enumerate
948 pit_type prev = depthHook(pit, pars_, par.getDepth());
950 && pars_[prev].layout() == layout
951 && pars_[prev].getDepth() == par.getDepth()
952 && pars_[prev].getLabelWidthString() == par.getLabelWidthString())
954 layoutasc = layout->itemsep * dh;
955 } else if (pit != 0 || row.pos() != 0) {
956 if (layout->topsep > 0)
957 layoutasc = layout->topsep * dh;
960 prev = outerHook(pit, pars_);
961 if (prev != pit_type(pars_.size())) {
962 maxasc += int(pars_[prev].layout()->parsep * dh);
963 } else if (pit != 0) {
964 if (pars_[pit - 1].getDepth() != 0 ||
965 pars_[pit - 1].layout() == layout) {
966 maxasc += int(layout->parsep * dh);
971 // is it a bottom line?
972 if (row.endpos() >= par.size()) {
973 // add the layout spaces, for example before and after
974 // a section, or between the items of a itemize or enumerate
976 pit_type nextpit = pit + 1;
977 if (nextpit != pit_type(pars_.size())) {
982 if (pars_[cpit].getDepth() > pars_[nextpit].getDepth()) {
983 usual = pars_[cpit].layout()->bottomsep * dh;
984 cpit = depthHook(cpit, paragraphs(), pars_[nextpit].getDepth());
985 if (pars_[cpit].layout() != pars_[nextpit].layout()
986 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
988 unusual = pars_[cpit].layout()->bottomsep * dh;
990 layoutdesc = max(unusual, usual);
991 } else if (pars_[cpit].getDepth() == pars_[nextpit].getDepth()) {
992 if (pars_[cpit].layout() != pars_[nextpit].layout()
993 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
994 layoutdesc = int(pars_[cpit].layout()->bottomsep * dh);
999 // incalculate the layout spaces
1000 maxasc += int(layoutasc * 2 / (2 + pars_[pit].getDepth()));
1001 maxdesc += int(layoutdesc * 2 / (2 + pars_[pit].getDepth()));
1003 row.ascent(maxasc + labeladdon);
1004 row.descent(maxdesc);
1012 void LyXText::breakParagraph(LCursor & cur, char keep_layout)
1014 BOOST_ASSERT(this == cur.text());
1015 // allow only if at start or end, or all previous is new text
1016 Paragraph & cpar = cur.paragraph();
1017 pit_type cpit = cur.pit();
1019 if (cur.pos() != 0 && cur.pos() != cur.lastpos()
1020 && cpar.isChangeEdited(0, cur.pos()))
1023 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
1024 LyXLayout_ptr const & layout = cpar.layout();
1026 // this is only allowed, if the current paragraph is not empty
1027 // or caption and if it has not the keepempty flag active
1028 if (cur.lastpos() == 0 && !cpar.allowEmpty()
1029 && layout->labeltype != LABEL_SENSITIVE)
1032 // a layout change may affect also the following paragraph
1033 recUndo(cur.pit(), undoSpan(cur.pit()) - 1);
1035 // Always break behind a space
1036 // It is better to erase the space (Dekel)
1037 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
1038 cpar.erase(cur.pos());
1040 // break the paragraph
1044 keep_layout = layout->isEnvironment();
1046 // we need to set this before we insert the paragraph. IMO the
1047 // breakParagraph call should return a bool if it inserts the
1048 // paragraph before or behind and we should react on that one
1049 // but we can fix this in 1.3.0 (Jug 20020509)
1050 bool const isempty = cpar.allowEmpty() && cpar.empty();
1051 ::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
1052 cur.pos(), keep_layout);
1055 pit_type next_par = cpit + 1;
1057 // well this is the caption hack since one caption is really enough
1058 if (layout->labeltype == LABEL_SENSITIVE) {
1060 // set to standard-layout
1061 pars_[cpit].applyLayout(tclass.defaultLayout());
1063 // set to standard-layout
1064 pars_[next_par].applyLayout(tclass.defaultLayout());
1067 // if the cursor is at the beginning of a row without prior newline,
1069 // This touches only the screen-update. Otherwise we would may have
1070 // an empty row on the screen
1071 if (cur.pos() != 0 && cur.textRow().pos() == cur.pos()
1072 && !pars_[cpit].isNewline(cur.pos() - 1))
1077 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
1078 pars_[next_par].erase(0);
1082 // This check is necessary. Otherwise the new empty paragraph will
1083 // be deleted automatically. And it is more friendly for the user!
1084 if (cur.pos() != 0 || isempty)
1085 setCursor(cur, cur.pit() + 1, 0);
1087 setCursor(cur, cur.pit(), 0);
1091 // insert a character, moves all the following breaks in the
1092 // same Paragraph one to the right and make a rebreak
1093 void LyXText::insertChar(LCursor & cur, char c)
1095 BOOST_ASSERT(this == cur.text());
1096 BOOST_ASSERT(c != Paragraph::META_INSET);
1098 recordUndo(cur, Undo::INSERT);
1100 Paragraph & par = cur.paragraph();
1101 // try to remove this
1102 pit_type const pit = cur.pit();
1104 bool const freeSpacing = par.layout()->free_spacing ||
1105 par.isFreeSpacing();
1107 if (lyxrc.auto_number) {
1108 static string const number_operators = "+-/*";
1109 static string const number_unary_operators = "+-";
1110 static string const number_seperators = ".,:";
1112 if (current_font.number() == LyXFont::ON) {
1113 if (!IsDigit(c) && !contains(number_operators, c) &&
1114 !(contains(number_seperators, c) &&
1116 cur.pos() != cur.lastpos() &&
1117 getFont(par, cur.pos()).number() == LyXFont::ON &&
1118 getFont(par, cur.pos() - 1).number() == LyXFont::ON)
1120 number(cur); // Set current_font.number to OFF
1121 } else if (IsDigit(c) &&
1122 real_current_font.isVisibleRightToLeft()) {
1123 number(cur); // Set current_font.number to ON
1125 if (cur.pos() != 0) {
1126 char const c = par.getChar(cur.pos() - 1);
1127 if (contains(number_unary_operators, c) &&
1129 || par.isSeparator(cur.pos() - 2)
1130 || par.isNewline(cur.pos() - 2))
1132 setCharFont(pit, cur.pos() - 1, current_font);
1133 } else if (contains(number_seperators, c)
1135 && getFont(par, cur.pos() - 2).number() == LyXFont::ON) {
1136 setCharFont(pit, cur.pos() - 1, current_font);
1142 // First check, if there will be two blanks together or a blank at
1143 // the beginning of a paragraph.
1144 // I decided to handle blanks like normal characters, the main
1145 // difference are the special checks when calculating the row.fill
1146 // (blank does not count at the end of a row) and the check here
1148 // The bug is triggered when we type in a description environment:
1149 // The current_font is not changed when we go from label to main text
1150 // and it should (along with realtmpfont) when we type the space.
1151 // CHECK There is a bug here! (Asger)
1153 // store the current font. This is because of the use of cursor
1154 // movements. The moving cursor would refresh the current font
1155 LyXFont realtmpfont = real_current_font;
1156 LyXFont rawtmpfont = current_font;
1158 // When the free-spacing option is set for the current layout,
1159 // disable the double-space checking
1160 if (!freeSpacing && IsLineSeparatorChar(c)) {
1161 if (cur.pos() == 0) {
1162 static bool sent_space_message = false;
1163 if (!sent_space_message) {
1164 cur.message(_("You cannot insert a space at the "
1165 "beginning of a paragraph. Please read the Tutorial."));
1166 sent_space_message = true;
1170 BOOST_ASSERT(cur.pos() > 0);
1171 if (par.isLineSeparator(cur.pos() - 1)
1172 || par.isNewline(cur.pos() - 1)) {
1173 static bool sent_space_message = false;
1174 if (!sent_space_message) {
1175 cur.message(_("You cannot type two spaces this way. "
1176 "Please read the Tutorial."));
1177 sent_space_message = true;
1183 par.insertChar(cur.pos(), c, rawtmpfont);
1185 current_font = rawtmpfont;
1186 real_current_font = realtmpfont;
1187 setCursor(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1192 void LyXText::charInserted()
1194 // Here we call finishUndo for every 20 characters inserted.
1195 // This is from my experience how emacs does it. (Lgb)
1196 static unsigned int counter;
1207 LyXText::computeRowMetrics(pit_type const pit, Row const & row) const
1210 Paragraph const & par = pars_[pit];
1212 double w = dim_.wid - row.width();
1214 bool const is_rtl = isRTL(par);
1216 result.x = rightMargin(par);
1218 result.x = leftMargin(pit, row.pos());
1220 // is there a manual margin with a manual label
1221 LyXLayout_ptr const & layout = par.layout();
1223 if (layout->margintype == MARGIN_MANUAL
1224 && layout->labeltype == LABEL_MANUAL) {
1225 /// We might have real hfills in the label part
1226 int nlh = numberOfLabelHfills(par, row);
1228 // A manual label par (e.g. List) has an auto-hfill
1229 // between the label text and the body of the
1231 // But we don't want to do this auto hfill if the par
1236 if (nlh && !par.getLabelWidthString().empty())
1237 result.label_hfill = labelFill(par, row) / double(nlh);
1240 // are there any hfills in the row?
1241 int const nh = numberOfHfills(par, row);
1245 result.hfill = w / nh;
1246 // we don't have to look at the alignment if it is ALIGN_LEFT and
1247 // if the row is already larger then the permitted width as then
1248 // we force the LEFT_ALIGN'edness!
1249 } else if (int(row.width()) < maxwidth_) {
1250 // is it block, flushleft or flushright?
1251 // set x how you need it
1253 if (par.params().align() == LYX_ALIGN_LAYOUT)
1254 align = layout->align;
1256 align = par.params().align();
1258 // Display-style insets should always be on a centred row
1259 // The test on par.size() is to catch zero-size pars, which
1260 // would trigger the assert in Paragraph::getInset().
1261 //inset = par.size() ? par.getInset(row.pos()) : 0;
1263 && par.isInset(row.pos())
1264 && par.getInset(row.pos())->display())
1266 align = LYX_ALIGN_CENTER;
1270 case LYX_ALIGN_BLOCK: {
1271 int const ns = numberOfSeparators(par, row);
1272 bool disp_inset = false;
1273 if (row.endpos() < par.size()) {
1274 InsetBase const * in = par.getInset(row.endpos());
1276 disp_inset = in->display();
1278 // If we have separators, this is not the last row of a
1279 // par, does not end in newline, and is not row above a
1280 // display inset... then stretch it
1282 && row.endpos() < par.size()
1283 && !par.isNewline(row.endpos() - 1)
1286 result.separator = w / ns;
1287 } else if (is_rtl) {
1292 case LYX_ALIGN_RIGHT:
1295 case LYX_ALIGN_CENTER:
1301 bidi.computeTables(par, *bv()->buffer(), row);
1303 pos_type body_pos = par.beginOfBody();
1304 pos_type end = row.endpos();
1307 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1309 result.x += font_metrics::width(layout->labelsep, getLabelFont(par));
1310 if (body_pos <= end)
1311 result.x += result.label_hfill;
1319 // the cursor set functions have a special mechanism. When they
1320 // realize, that you left an empty paragraph, they will delete it.
1322 void LyXText::cursorRightOneWord(LCursor & cur)
1324 BOOST_ASSERT(this == cur.text());
1325 if (cur.pos() == cur.lastpos() && cur.pit() != cur.lastpit()) {
1329 // Skip through initial nonword stuff.
1330 // Treat floats and insets as words.
1331 while (cur.pos() != cur.lastpos() && !cur.paragraph().isLetter(cur.pos()))
1333 // Advance through word.
1334 while (cur.pos() != cur.lastpos() && cur.paragraph().isLetter(cur.pos()))
1337 setCursor(cur, cur.pit(), cur.pos());
1341 void LyXText::cursorLeftOneWord(LCursor & cur)
1343 BOOST_ASSERT(this == cur.text());
1344 if (cur.pos() == 0 && cur.pit() != 0) {
1346 cur.pos() = cur.lastpos();
1348 // Skip through initial nonword stuff.
1349 // Treat floats and insets as words.
1350 while (cur.pos() != 0 && !cur.paragraph().isLetter(cur.pos() - 1))
1352 // Advance through word.
1353 while (cur.pos() != 0 && cur.paragraph().isLetter(cur.pos() - 1))
1356 setCursor(cur, cur.pit(), cur.pos());
1360 void LyXText::selectWord(LCursor & cur, word_location loc)
1362 BOOST_ASSERT(this == cur.text());
1363 CursorSlice from = cur.top();
1364 CursorSlice to = cur.top();
1365 getWord(from, to, loc);
1366 if (cur.top() != from)
1367 setCursor(cur, from.pit(), from.pos());
1371 setCursor(cur, to.pit(), to.pos());
1376 // Select the word currently under the cursor when no
1377 // selection is currently set
1378 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1380 BOOST_ASSERT(this == cur.text());
1381 if (cur.selection())
1383 selectWord(cur, loc);
1384 return cur.selection();
1388 void LyXText::acceptChange(LCursor & cur)
1390 BOOST_ASSERT(this == cur.text());
1391 if (!cur.selection() && cur.lastpos() != 0)
1394 CursorSlice const & startc = cur.selBegin();
1395 CursorSlice const & endc = cur.selEnd();
1396 if (startc.pit() == endc.pit()) {
1397 recordUndoSelection(cur, Undo::INSERT);
1398 pars_[startc.pit()].acceptChange(startc.pos(), endc.pos());
1400 cur.clearSelection();
1401 setCursorIntern(cur, startc.pit(), 0);
1403 #ifdef WITH_WARNINGS
1404 #warning handle multi par selection
1409 void LyXText::rejectChange(LCursor & cur)
1411 BOOST_ASSERT(this == cur.text());
1412 if (!cur.selection() && cur.lastpos() != 0)
1415 CursorSlice const & startc = cur.selBegin();
1416 CursorSlice const & endc = cur.selEnd();
1417 if (startc.pit() == endc.pit()) {
1418 recordUndoSelection(cur, Undo::INSERT);
1419 pars_[startc.pit()].rejectChange(startc.pos(), endc.pos());
1421 cur.clearSelection();
1422 setCursorIntern(cur, startc.pit(), 0);
1424 #ifdef WITH_WARNINGS
1425 #warning handle multi par selection
1430 // Delete from cursor up to the end of the current or next word.
1431 void LyXText::deleteWordForward(LCursor & cur)
1433 BOOST_ASSERT(this == cur.text());
1434 if (cur.lastpos() == 0)
1438 cur.selection() = true;
1439 cursorRightOneWord(cur);
1441 cutSelection(cur, true, false);
1446 // Delete from cursor to start of current or prior word.
1447 void LyXText::deleteWordBackward(LCursor & cur)
1449 BOOST_ASSERT(this == cur.text());
1450 if (cur.lastpos() == 0)
1454 cur.selection() = true;
1455 cursorLeftOneWord(cur);
1457 cutSelection(cur, true, false);
1462 // Kill to end of line.
1463 void LyXText::deleteLineForward(LCursor & cur)
1465 BOOST_ASSERT(this == cur.text());
1466 if (cur.lastpos() == 0) {
1467 // Paragraph is empty, so we just go to the right
1471 cur.selection() = true; // to avoid deletion
1474 // What is this test for ??? (JMarc)
1475 if (!cur.selection())
1476 deleteWordForward(cur);
1478 cutSelection(cur, true, false);
1483 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1485 BOOST_ASSERT(this == cur.text());
1489 if (cur.selection()) {
1490 from = cur.selBegin();
1494 getWord(from, to, lyx::PARTIAL_WORD);
1495 setCursor(cur, to.pit(), to.pos() + 1);
1498 recordUndoSelection(cur);
1500 pos_type pos = from.pos();
1501 int par = from.pit();
1503 while (par != int(pars_.size()) && (pos != to.pos() || par != to.pit())) {
1505 if (pos == pars_[pit].size()) {
1510 unsigned char c = pars_[pit].getChar(pos);
1511 if (c != Paragraph::META_INSET) {
1513 case text_lowercase:
1516 case text_capitalization:
1518 action = text_lowercase;
1520 case text_uppercase:
1525 #ifdef WITH_WARNINGS
1528 pars_[pit].setChar(pos, c);
1534 void LyXText::Delete(LCursor & cur)
1536 BOOST_ASSERT(this == cur.text());
1537 if (cur.pos() != cur.lastpos()) {
1538 recordUndo(cur, Undo::DELETE, cur.pit());
1539 setCursorIntern(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1542 // should we do anything in an else branch?
1546 void LyXText::backspace(LCursor & cur)
1548 BOOST_ASSERT(this == cur.text());
1549 if (cur.pos() == 0) {
1550 // The cursor is at the beginning of a paragraph, so
1551 // the the backspace will collapse two paragraphs into
1554 // but it's not allowed unless it's new
1555 Paragraph & par = cur.paragraph();
1556 if (par.isChangeEdited(0, par.size()))
1559 // we may paste some paragraphs
1561 // is it an empty paragraph?
1562 pos_type lastpos = cur.lastpos();
1563 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1564 // This is an empty paragraph and we delete it just
1565 // by moving the cursor one step
1566 // left and let the DeleteEmptyParagraphMechanism
1567 // handle the actual deletion of the paragraph.
1569 if (cur.pit() != 0) {
1576 recordUndo(cur, Undo::DELETE, cur.pit() - 1);
1578 pit_type tmppit = cur.pit();
1579 // We used to do cursorLeftIntern() here, but it is
1580 // not a good idea since it triggers the auto-delete
1581 // mechanism. So we do a cursorLeftIntern()-lite,
1582 // without the dreaded mechanism. (JMarc)
1583 if (cur.pit() != 0) {
1584 // steps into the above paragraph.
1585 setCursorIntern(cur, cur.pit() - 1,
1586 pars_[cur.pit() - 1].size(),
1590 // Pasting is not allowed, if the paragraphs have different
1591 // layout. I think it is a real bug of all other
1592 // word processors to allow it. It confuses the user.
1593 // Correction: Pasting is always allowed with standard-layout
1594 Buffer & buf = cur.buffer();
1595 BufferParams const & bufparams = buf.params();
1596 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1597 pit_type const cpit = cur.pit();
1600 && (pars_[cpit].layout() == pars_[tmppit].layout()
1601 || pars_[tmppit].layout() == tclass.defaultLayout())
1602 && pars_[cpit].getAlign() == pars_[tmppit].getAlign()) {
1603 mergeParagraph(bufparams, pars_, cpit);
1605 if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
1608 // the counters may have changed
1610 setCursor(cur, cur.pit(), cur.pos(), false);
1613 // this is the code for a normal backspace, not pasting
1615 recordUndo(cur, Undo::DELETE);
1616 // We used to do cursorLeftIntern() here, but it is
1617 // not a good idea since it triggers the auto-delete
1618 // mechanism. So we do a cursorLeftIntern()-lite,
1619 // without the dreaded mechanism. (JMarc)
1620 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1621 false, cur.boundary());
1622 cur.paragraph().erase(cur.pos());
1625 if (cur.pos() == cur.lastpos())
1626 setCurrentFont(cur);
1628 setCursor(cur, cur.pit(), cur.pos(), false, cur.boundary());
1632 Paragraph & LyXText::getPar(pit_type par) const
1634 //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
1635 BOOST_ASSERT(par >= 0);
1636 BOOST_ASSERT(par < int(paragraphs().size()));
1637 return paragraphs()[par];
1641 Row const & LyXText::firstRow() const
1643 return *paragraphs().front().rows().begin();
1647 void LyXText::redoParagraph(pit_type const pit)
1649 // remove rows of paragraph, keep track of height changes
1650 Paragraph & par = pars_[pit];
1653 InsetList::iterator ii = par.insetlist.begin();
1654 InsetList::iterator iend = par.insetlist.end();
1655 for (; ii != iend; ++ii) {
1657 int const w = maxwidth_ - leftMargin(pit) - rightMargin(par);
1658 MetricsInfo mi(bv(), getFont(par, ii->pos), w);
1659 ii->inset->metrics(mi, dim);
1662 // rebreak the paragraph
1666 par.setBeginOfBody();
1670 rowBreakPoint(pit, row);
1671 setRowWidth(pit, row);
1672 setHeightOfRow(pit, row);
1673 par.rows().push_back(row);
1674 dim.wid = std::max(dim.wid, row.width());
1675 dim.des += row.height();
1677 } while (z < par.size());
1679 dim.asc += par.rows()[0].ascent();
1680 dim.des -= par.rows()[0].ascent();
1682 //lyxerr << "redoParagraph: " << par.rows().size() << " rows\n";
1686 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1688 //BOOST_ASSERT(mi.base.textwidth);
1689 if (mi.base.textwidth)
1690 maxwidth_ = mi.base.textwidth;
1691 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1692 // << " maxWidth: " << maxwidth_ << "\nfont: " << mi.base.font << endl;
1696 for (pit_type pit = 0, n = paragraphs().size(); pit != n; ++pit) {
1698 Paragraph & par = paragraphs()[pit];
1700 if (w < par.width())
1705 dim.asc = pars_[0].ascent();
1706 dim.des = h - dim.asc;
1712 // only used for inset right now. should also be used for main text
1713 void LyXText::draw(PainterInfo & pi, int x, int y) const
1715 paintTextInset(*this, pi, x, y);
1720 // only used for inset right now. should also be used for main text
1721 void LyXText::drawSelection(PainterInfo & pi, int x , int) const
1723 LCursor & cur = pi.base.bv->cursor();
1724 if (!cur.selection())
1726 if (!ptr_cmp(cur.text(), this))
1729 lyxerr << "draw selection at " << x << endl;
1731 // is there a better way of getting these two iterators?
1732 DocIterator beg = cur;
1733 DocIterator end = cur;
1735 beg.top() = cur.selBegin();
1736 end.top() = cur.selEnd();
1738 // the selection doesn't touch the visible screen
1739 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
1740 || bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_ABOVE)
1743 Paragraph const & par1 = pars_[beg.pit()];
1744 Paragraph const & par2 = pars_[end.pit()];
1746 Row const & row1 = par1.getRow(beg.pos());
1747 Row const & row2 = par2.getRow(end.pos());
1750 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_ABOVE) {
1755 y1 = bv_funcs::getPos(beg).y_ - row1.ascent();
1756 int const startx = cursorX(beg.top());
1757 x1 = isRTL(par1) ? startx : 0;
1758 x2 = isRTL(par1) ? 0 + dim_.wid : startx;
1762 if (bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_BELOW) {
1763 y2 = pi.base.bv->workHeight();
1767 y2 = bv_funcs::getPos(end).y_ + row2.descent();
1768 int const endx = cursorX(end.top());
1769 X1 = isRTL(par2) ? 0 : endx;
1770 X2 = isRTL(par2) ? endx : 0 + dim_.wid;
1773 lyxerr << " y1: " << y1 << " y2: " << y2
1774 << " xo: " << xo_ << " wid: " << dim_.wid
1777 // paint big rectangle in one go
1778 pi.pain.fillRectangle(x, y1, dim_.wid, y2 - y1, LColor::selection);
1780 // reset background at begin of first selected line
1781 pi.pain.fillRectangle(x + x1, y1, x2 - x1, row1.height(),
1782 LColor::background);
1784 // reset background at end of last selected line
1785 pi.pain.fillRectangle(x + X1, y2 - row2.height(),
1786 X2 - X1, row2.height(), LColor::background);
1791 void LyXText::drawSelection(PainterInfo & pi, int x, int) const
1793 LCursor & cur = pi.base.bv->cursor();
1794 if (!cur.selection())
1796 if (!ptr_cmp(cur.text(), this))
1799 lyxerr << "draw selection at " << x << endl;
1801 // is there a better way of getting these two iterators?
1802 DocIterator beg = cur;
1803 DocIterator end = cur;
1805 beg.top() = cur.selBegin();
1806 end.top() = cur.selEnd();
1808 // the selection doesn't touch the visible screen
1809 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
1810 || bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_ABOVE)
1813 Paragraph const & par1 = pars_[beg.pit()];
1814 Paragraph const & par2 = pars_[end.pit()];
1816 bool const above = (bv_funcs::status(pi.base.bv, beg)
1817 == bv_funcs::CUR_ABOVE);
1818 bool const below = (bv_funcs::status(pi.base.bv, end)
1819 == bv_funcs::CUR_BELOW);
1827 Row const & row1 = par1.getRow(beg.pos());
1828 y1 = bv_funcs::getPos(beg).y_ - row1.ascent();
1829 y2 = y1 + row1.height();
1830 int const startx = cursorX(beg.top());
1831 x1 = !isRTL(par1) ? startx : 0;
1832 x2 = !isRTL(par1) ? 0 + dim_.wid : startx;
1837 Y1 = pi.base.bv->workHeight();
1838 Y2 = pi.base.bv->workHeight();
1842 Row const & row2 = par2.getRow(end.pos());
1843 Y1 = bv_funcs::getPos(end).y_ - row2.ascent();
1844 Y2 = Y1 + row2.height();
1845 int const endx = cursorX(end.top());
1846 X1 = !isRTL(par2) ? 0 : endx;
1847 X2 = !isRTL(par2) ? endx : 0 + dim_.wid;
1850 if (!above && !below && &par1.getRow(beg.pos())
1851 == &par2.getRow(end.pos()))
1853 // paint only one rectangle
1854 pi.pain.fillRectangle(x + x1, y1, X2 - x1, y2 - y1,
1859 // paint upper rectangle
1860 pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
1862 // paint bottom rectangle
1863 pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
1865 // paint center rectangle
1866 pi.pain.fillRectangle(x, y2, dim_.wid,
1867 Y1 - y2, LColor::selection);
1870 bool LyXText::isLastRow(pit_type pit, Row const & row) const
1872 return row.endpos() >= pars_[pit].size()
1873 && pit + 1 == pit_type(paragraphs().size());
1877 bool LyXText::isFirstRow(pit_type pit, Row const & row) const
1879 return row.pos() == 0 && pit == 0;
1883 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1884 word_location const loc)
1886 Paragraph const & from_par = pars_[from.pit()];
1888 case lyx::WHOLE_WORD_STRICT:
1889 if (from.pos() == 0 || from.pos() == from_par.size()
1890 || !from_par.isLetter(from.pos())
1891 || !from_par.isLetter(from.pos() - 1)) {
1895 // no break here, we go to the next
1897 case lyx::WHOLE_WORD:
1898 // If we are already at the beginning of a word, do nothing
1899 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
1901 // no break here, we go to the next
1903 case lyx::PREVIOUS_WORD:
1904 // always move the cursor to the beginning of previous word
1905 while (from.pos() && from_par.isLetter(from.pos() - 1))
1908 case lyx::NEXT_WORD:
1909 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1912 case lyx::PARTIAL_WORD:
1913 // no need to move the 'from' cursor
1917 Paragraph & to_par = pars_[to.pit()];
1918 while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
1923 void LyXText::write(Buffer const & buf, std::ostream & os) const
1925 ParagraphList::const_iterator pit = paragraphs().begin();
1926 ParagraphList::const_iterator end = paragraphs().end();
1927 Paragraph::depth_type dth = 0;
1928 for (; pit != end; ++pit)
1929 pit->write(buf, os, buf.params(), dth);
1933 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1935 static Change current_change;
1937 Paragraph::depth_type depth = 0;
1939 while (lex.isOK()) {
1941 string const token = lex.getString();
1946 if (token == "\\end_inset") {
1950 if (token == "\\end_body") {
1954 if (token == "\\begin_body") {
1958 if (token == "\\end_document") {
1962 if (token == "\\begin_layout") {
1963 lex.pushToken(token);
1966 par.params().depth(depth);
1967 if (buf.params().tracking_changes)
1969 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1970 pars_.push_back(par);
1972 // FIXME: goddamn InsetTabular makes us pass a Buffer
1974 ::readParagraph(buf, pars_.back(), lex);
1976 } else if (token == "\\begin_deeper") {
1978 } else if (token == "\\end_deeper") {
1980 lex.printError("\\end_deeper: " "depth is already null");
1985 lyxerr << "Handling unknown body token: `"
1986 << token << '\'' << endl;
1993 int LyXText::ascent() const
1999 int LyXText::descent() const
2005 int LyXText::cursorX(CursorSlice const & cur) const
2007 pit_type const pit = cur.pit();
2008 Paragraph const & par = pars_[pit];
2009 if (par.rows().empty())
2012 Row const & row = par.getRow(cur.pos());
2014 pos_type pos = cur.pos();
2015 pos_type cursor_vpos = 0;
2017 RowMetrics const m = computeRowMetrics(pit, row);
2020 pos_type const row_pos = row.pos();
2021 pos_type const end = row.endpos();
2024 cursor_vpos = row_pos;
2025 else if (pos >= end)
2026 cursor_vpos = isRTL(par) ? row_pos : end;
2027 else if (pos > row_pos && pos >= end)
2028 // Place cursor after char at (logical) position pos - 1
2029 cursor_vpos = (bidi.level(pos - 1) % 2 == 0)
2030 ? bidi.log2vis(pos - 1) + 1 : bidi.log2vis(pos - 1);
2032 // Place cursor before char at (logical) position pos
2033 cursor_vpos = (bidi.level(pos) % 2 == 0)
2034 ? bidi.log2vis(pos) : bidi.log2vis(pos) + 1;
2036 pos_type body_pos = par.beginOfBody();
2038 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
2041 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
2042 pos_type pos = bidi.vis2log(vpos);
2043 if (body_pos > 0 && pos == body_pos - 1) {
2045 + font_metrics::width(par.layout()->labelsep,
2047 if (par.isLineSeparator(body_pos - 1))
2048 x -= singleWidth(par, body_pos - 1);
2051 if (hfillExpansion(par, row, pos)) {
2052 x += singleWidth(par, pos);
2053 if (pos >= body_pos)
2057 } else if (par.isSeparator(pos)) {
2058 x += singleWidth(par, pos);
2059 if (pos >= body_pos)
2062 x += singleWidth(par, pos);
2068 int LyXText::cursorY(CursorSlice const & cur) const
2070 Paragraph const & par = getPar(cur.pit());
2072 h -= pars_[0].rows()[0].ascent();
2073 for (pit_type pit = 0; pit < cur.pit(); ++pit)
2074 h += pars_[pit].height();
2075 for (size_t rit = 0, rend = par.pos2row(cur.pos()); rit != rend; ++rit)
2076 h += par.rows()[rit].height();
2077 h += par.rows()[par.pos2row(cur.pos())].ascent();
2082 // Returns the current font and depth as a message.
2083 string LyXText::currentState(LCursor & cur)
2085 BOOST_ASSERT(this == cur.text());
2086 Buffer & buf = cur.buffer();
2087 Paragraph const & par = cur.paragraph();
2088 std::ostringstream os;
2090 bool const show_change = buf.params().tracking_changes
2091 && cur.pos() != cur.lastpos()
2092 && par.lookupChange(cur.pos()) != Change::UNCHANGED;
2095 Change change = par.lookupChangeFull(cur.pos());
2096 Author const & a = buf.params().authors().get(change.author);
2097 os << _("Change: ") << a.name();
2098 if (!a.email().empty())
2099 os << " (" << a.email() << ")";
2100 if (change.changetime)
2101 os << _(" at ") << ctime(&change.changetime);
2105 // I think we should only show changes from the default
2107 LyXFont font = real_current_font;
2108 font.reduce(buf.params().getLyXTextClass().defaultfont());
2110 // avoid _(...) re-entrance problem
2111 string const s = font.stateText(&buf.params());
2112 os << bformat(_("Font: %1$s"), s);
2114 // os << bformat(_("Font: %1$s"), font.stateText(&buf.params));
2116 // The paragraph depth
2117 int depth = cur.paragraph().getDepth();
2119 os << bformat(_(", Depth: %1$d"), depth);
2121 // The paragraph spacing, but only if different from
2123 Spacing const & spacing = par.params().spacing();
2124 if (!spacing.isDefault()) {
2125 os << _(", Spacing: ");
2126 switch (spacing.getSpace()) {
2127 case Spacing::Single:
2130 case Spacing::Onehalf:
2133 case Spacing::Double:
2136 case Spacing::Other:
2137 os << _("Other (") << spacing.getValueAsString() << ')';
2139 case Spacing::Default:
2140 // should never happen, do nothing
2145 #ifdef DEVEL_VERSION
2146 os << _(", Inset: ") << &cur.inset();
2147 os << _(", Paragraph: ") << cur.pit();
2148 os << _(", Id: ") << par.id();
2149 os << _(", Position: ") << cur.pos();
2150 // Row & row = cur.textRow();
2151 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2157 string LyXText::getPossibleLabel(LCursor & cur) const
2159 pit_type pit = cur.pit();
2161 LyXLayout_ptr layout = pars_[pit].layout();
2163 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2164 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
2165 if (layout2->latextype != LATEX_PARAGRAPH) {
2171 string text = layout->latexname().substr(0, 3);
2172 if (layout->latexname() == "theorem")
2173 text = "thm"; // Create a correct prefix for prettyref
2176 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2179 string par_text = pars_[pit].asString(cur.buffer(), false);
2180 for (int i = 0; i < lyxrc.label_init_length; ++i) {
2181 if (par_text.empty())
2184 par_text = split(par_text, head, ' ');
2185 // Is it legal to use spaces in labels ?
2195 //pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2199 // Paragraph const & par = pars_[pit];
2200 // Row const & r = par.rows()[row];
2201 // int pos = r.pos();
2202 // for (; currx < x && pos < r.endpos(); ++pos) {
2204 // currx += singleWidth(par, pos);
2206 // if (abs(lastx - x) < abs(currx - x) && pos != r.pos())
2212 pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2215 Row const & r = pars_[pit].rows()[row];
2216 return r.pos() + getColumnNearX(pit, r, x, bound);
2220 //int LyXText::pos2x(pit_type pit, pos_type pos) const
2222 // Paragraph const & par = pars_[pit];
2223 // Row const & r = par.rows()[row];
2229 // x,y are screen coordinates
2230 // sets cursor only within this LyXText
2231 void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
2233 pit_type pit = getPitNearY(y);
2234 int yy = theCoords.get(this, pit).y_ - pars_[pit].ascent();
2235 lyxerr << "setCursorFromCoordinates: x: " << x << " y: " << y
2236 << " pit: " << pit << " yy: " << yy << endl;
2238 Paragraph const & par = pars_[pit];
2240 BOOST_ASSERT(par.rows().size());
2241 for (; r < int(par.rows().size()) - 1; ++r) {
2242 Row const & row = par.rows()[r];
2243 if (int(yy + row.height()) > y)
2248 Row const & row = par.rows()[r];
2250 lyxerr << "setCursorFromCoordinates: row " << r
2251 << " from pos: " << row.pos() << endl;
2255 pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
2256 setCursor(cur, pit, pos, true, bound);