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/convert.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 // Top and bottom margin of the document (only at top-level)
1004 if (bv_owner->text() == this) {
1005 if (pit == 0 && row.pos() == 0)
1007 if (pit == pars_.size() - 1 && row.endpos() == par.size())
1011 row.ascent(maxasc + labeladdon);
1012 row.descent(maxdesc);
1020 void LyXText::breakParagraph(LCursor & cur, char keep_layout)
1022 BOOST_ASSERT(this == cur.text());
1023 // allow only if at start or end, or all previous is new text
1024 Paragraph & cpar = cur.paragraph();
1025 pit_type cpit = cur.pit();
1027 if (cur.pos() != 0 && cur.pos() != cur.lastpos()
1028 && cpar.isChangeEdited(0, cur.pos()))
1031 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
1032 LyXLayout_ptr const & layout = cpar.layout();
1034 // this is only allowed, if the current paragraph is not empty
1035 // or caption and if it has not the keepempty flag active
1036 if (cur.lastpos() == 0 && !cpar.allowEmpty()
1037 && layout->labeltype != LABEL_SENSITIVE)
1040 // a layout change may affect also the following paragraph
1041 recUndo(cur.pit(), undoSpan(cur.pit()) - 1);
1043 // Always break behind a space
1044 // It is better to erase the space (Dekel)
1045 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
1046 cpar.erase(cur.pos());
1048 // break the paragraph
1052 keep_layout = layout->isEnvironment();
1054 // we need to set this before we insert the paragraph. IMO the
1055 // breakParagraph call should return a bool if it inserts the
1056 // paragraph before or behind and we should react on that one
1057 // but we can fix this in 1.3.0 (Jug 20020509)
1058 bool const isempty = cpar.allowEmpty() && cpar.empty();
1059 ::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
1060 cur.pos(), keep_layout);
1063 pit_type next_par = cpit + 1;
1065 // well this is the caption hack since one caption is really enough
1066 if (layout->labeltype == LABEL_SENSITIVE) {
1068 // set to standard-layout
1069 pars_[cpit].applyLayout(tclass.defaultLayout());
1071 // set to standard-layout
1072 pars_[next_par].applyLayout(tclass.defaultLayout());
1075 // if the cursor is at the beginning of a row without prior newline,
1077 // This touches only the screen-update. Otherwise we would may have
1078 // an empty row on the screen
1079 if (cur.pos() != 0 && cur.textRow().pos() == cur.pos()
1080 && !pars_[cpit].isNewline(cur.pos() - 1))
1085 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
1086 pars_[next_par].erase(0);
1090 // This check is necessary. Otherwise the new empty paragraph will
1091 // be deleted automatically. And it is more friendly for the user!
1092 if (cur.pos() != 0 || isempty)
1093 setCursor(cur, cur.pit() + 1, 0);
1095 setCursor(cur, cur.pit(), 0);
1099 // insert a character, moves all the following breaks in the
1100 // same Paragraph one to the right and make a rebreak
1101 void LyXText::insertChar(LCursor & cur, char c)
1103 BOOST_ASSERT(this == cur.text());
1104 BOOST_ASSERT(c != Paragraph::META_INSET);
1106 recordUndo(cur, Undo::INSERT);
1108 Paragraph & par = cur.paragraph();
1109 // try to remove this
1110 pit_type const pit = cur.pit();
1112 bool const freeSpacing = par.layout()->free_spacing ||
1113 par.isFreeSpacing();
1115 if (lyxrc.auto_number) {
1116 static string const number_operators = "+-/*";
1117 static string const number_unary_operators = "+-";
1118 static string const number_seperators = ".,:";
1120 if (current_font.number() == LyXFont::ON) {
1121 if (!IsDigit(c) && !contains(number_operators, c) &&
1122 !(contains(number_seperators, c) &&
1124 cur.pos() != cur.lastpos() &&
1125 getFont(par, cur.pos()).number() == LyXFont::ON &&
1126 getFont(par, cur.pos() - 1).number() == LyXFont::ON)
1128 number(cur); // Set current_font.number to OFF
1129 } else if (IsDigit(c) &&
1130 real_current_font.isVisibleRightToLeft()) {
1131 number(cur); // Set current_font.number to ON
1133 if (cur.pos() != 0) {
1134 char const c = par.getChar(cur.pos() - 1);
1135 if (contains(number_unary_operators, c) &&
1137 || par.isSeparator(cur.pos() - 2)
1138 || par.isNewline(cur.pos() - 2))
1140 setCharFont(pit, cur.pos() - 1, current_font);
1141 } else if (contains(number_seperators, c)
1143 && getFont(par, cur.pos() - 2).number() == LyXFont::ON) {
1144 setCharFont(pit, cur.pos() - 1, current_font);
1150 // First check, if there will be two blanks together or a blank at
1151 // the beginning of a paragraph.
1152 // I decided to handle blanks like normal characters, the main
1153 // difference are the special checks when calculating the row.fill
1154 // (blank does not count at the end of a row) and the check here
1156 // The bug is triggered when we type in a description environment:
1157 // The current_font is not changed when we go from label to main text
1158 // and it should (along with realtmpfont) when we type the space.
1159 // CHECK There is a bug here! (Asger)
1161 // store the current font. This is because of the use of cursor
1162 // movements. The moving cursor would refresh the current font
1163 LyXFont realtmpfont = real_current_font;
1164 LyXFont rawtmpfont = current_font;
1166 // When the free-spacing option is set for the current layout,
1167 // disable the double-space checking
1168 if (!freeSpacing && IsLineSeparatorChar(c)) {
1169 if (cur.pos() == 0) {
1170 static bool sent_space_message = false;
1171 if (!sent_space_message) {
1172 cur.message(_("You cannot insert a space at the "
1173 "beginning of a paragraph. Please read the Tutorial."));
1174 sent_space_message = true;
1178 BOOST_ASSERT(cur.pos() > 0);
1179 if (par.isLineSeparator(cur.pos() - 1)
1180 || par.isNewline(cur.pos() - 1)) {
1181 static bool sent_space_message = false;
1182 if (!sent_space_message) {
1183 cur.message(_("You cannot type two spaces this way. "
1184 "Please read the Tutorial."));
1185 sent_space_message = true;
1191 par.insertChar(cur.pos(), c, rawtmpfont);
1193 current_font = rawtmpfont;
1194 real_current_font = realtmpfont;
1195 setCursor(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1200 void LyXText::charInserted()
1202 // Here we call finishUndo for every 20 characters inserted.
1203 // This is from my experience how emacs does it. (Lgb)
1204 static unsigned int counter;
1215 LyXText::computeRowMetrics(pit_type const pit, Row const & row) const
1218 Paragraph const & par = pars_[pit];
1220 double w = dim_.wid - row.width();
1222 bool const is_rtl = isRTL(par);
1224 result.x = rightMargin(par);
1226 result.x = leftMargin(pit, row.pos());
1228 // is there a manual margin with a manual label
1229 LyXLayout_ptr const & layout = par.layout();
1231 if (layout->margintype == MARGIN_MANUAL
1232 && layout->labeltype == LABEL_MANUAL) {
1233 /// We might have real hfills in the label part
1234 int nlh = numberOfLabelHfills(par, row);
1236 // A manual label par (e.g. List) has an auto-hfill
1237 // between the label text and the body of the
1239 // But we don't want to do this auto hfill if the par
1244 if (nlh && !par.getLabelWidthString().empty())
1245 result.label_hfill = labelFill(par, row) / double(nlh);
1248 // are there any hfills in the row?
1249 int const nh = numberOfHfills(par, row);
1253 result.hfill = w / nh;
1254 // we don't have to look at the alignment if it is ALIGN_LEFT and
1255 // if the row is already larger then the permitted width as then
1256 // we force the LEFT_ALIGN'edness!
1257 } else if (int(row.width()) < maxwidth_) {
1258 // is it block, flushleft or flushright?
1259 // set x how you need it
1261 if (par.params().align() == LYX_ALIGN_LAYOUT)
1262 align = layout->align;
1264 align = par.params().align();
1266 // Display-style insets should always be on a centred row
1267 // The test on par.size() is to catch zero-size pars, which
1268 // would trigger the assert in Paragraph::getInset().
1269 //inset = par.size() ? par.getInset(row.pos()) : 0;
1271 && par.isInset(row.pos())
1272 && par.getInset(row.pos())->display())
1274 align = LYX_ALIGN_CENTER;
1278 case LYX_ALIGN_BLOCK: {
1279 int const ns = numberOfSeparators(par, row);
1280 bool disp_inset = false;
1281 if (row.endpos() < par.size()) {
1282 InsetBase const * in = par.getInset(row.endpos());
1284 disp_inset = in->display();
1286 // If we have separators, this is not the last row of a
1287 // par, does not end in newline, and is not row above a
1288 // display inset... then stretch it
1290 && row.endpos() < par.size()
1291 && !par.isNewline(row.endpos() - 1)
1294 result.separator = w / ns;
1295 } else if (is_rtl) {
1300 case LYX_ALIGN_RIGHT:
1303 case LYX_ALIGN_CENTER:
1309 bidi.computeTables(par, *bv()->buffer(), row);
1311 pos_type body_pos = par.beginOfBody();
1312 pos_type end = row.endpos();
1315 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1317 result.x += font_metrics::width(layout->labelsep, getLabelFont(par));
1318 if (body_pos <= end)
1319 result.x += result.label_hfill;
1327 // the cursor set functions have a special mechanism. When they
1328 // realize, that you left an empty paragraph, they will delete it.
1330 void LyXText::cursorRightOneWord(LCursor & cur)
1332 BOOST_ASSERT(this == cur.text());
1333 if (cur.pos() == cur.lastpos() && cur.pit() != cur.lastpit()) {
1337 // Skip through initial nonword stuff.
1338 // Treat floats and insets as words.
1339 while (cur.pos() != cur.lastpos() && !cur.paragraph().isLetter(cur.pos()))
1341 // Advance through word.
1342 while (cur.pos() != cur.lastpos() && cur.paragraph().isLetter(cur.pos()))
1345 setCursor(cur, cur.pit(), cur.pos());
1349 void LyXText::cursorLeftOneWord(LCursor & cur)
1351 BOOST_ASSERT(this == cur.text());
1352 if (cur.pos() == 0 && cur.pit() != 0) {
1354 cur.pos() = cur.lastpos();
1356 // Skip through initial nonword stuff.
1357 // Treat floats and insets as words.
1358 while (cur.pos() != 0 && !cur.paragraph().isLetter(cur.pos() - 1))
1360 // Advance through word.
1361 while (cur.pos() != 0 && cur.paragraph().isLetter(cur.pos() - 1))
1364 setCursor(cur, cur.pit(), cur.pos());
1368 void LyXText::selectWord(LCursor & cur, word_location loc)
1370 BOOST_ASSERT(this == cur.text());
1371 CursorSlice from = cur.top();
1372 CursorSlice to = cur.top();
1373 getWord(from, to, loc);
1374 if (cur.top() != from)
1375 setCursor(cur, from.pit(), from.pos());
1379 setCursor(cur, to.pit(), to.pos());
1384 // Select the word currently under the cursor when no
1385 // selection is currently set
1386 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1388 BOOST_ASSERT(this == cur.text());
1389 if (cur.selection())
1391 selectWord(cur, loc);
1392 return cur.selection();
1396 void LyXText::acceptChange(LCursor & cur)
1398 BOOST_ASSERT(this == cur.text());
1399 if (!cur.selection() && cur.lastpos() != 0)
1402 CursorSlice const & startc = cur.selBegin();
1403 CursorSlice const & endc = cur.selEnd();
1404 if (startc.pit() == endc.pit()) {
1405 recordUndoSelection(cur, Undo::INSERT);
1406 pars_[startc.pit()].acceptChange(startc.pos(), endc.pos());
1408 cur.clearSelection();
1409 setCursorIntern(cur, startc.pit(), 0);
1411 #ifdef WITH_WARNINGS
1412 #warning handle multi par selection
1417 void LyXText::rejectChange(LCursor & cur)
1419 BOOST_ASSERT(this == cur.text());
1420 if (!cur.selection() && cur.lastpos() != 0)
1423 CursorSlice const & startc = cur.selBegin();
1424 CursorSlice const & endc = cur.selEnd();
1425 if (startc.pit() == endc.pit()) {
1426 recordUndoSelection(cur, Undo::INSERT);
1427 pars_[startc.pit()].rejectChange(startc.pos(), endc.pos());
1429 cur.clearSelection();
1430 setCursorIntern(cur, startc.pit(), 0);
1432 #ifdef WITH_WARNINGS
1433 #warning handle multi par selection
1438 // Delete from cursor up to the end of the current or next word.
1439 void LyXText::deleteWordForward(LCursor & cur)
1441 BOOST_ASSERT(this == cur.text());
1442 if (cur.lastpos() == 0)
1446 cur.selection() = true;
1447 cursorRightOneWord(cur);
1449 cutSelection(cur, true, false);
1454 // Delete from cursor to start of current or prior word.
1455 void LyXText::deleteWordBackward(LCursor & cur)
1457 BOOST_ASSERT(this == cur.text());
1458 if (cur.lastpos() == 0)
1462 cur.selection() = true;
1463 cursorLeftOneWord(cur);
1465 cutSelection(cur, true, false);
1470 // Kill to end of line.
1471 void LyXText::deleteLineForward(LCursor & cur)
1473 BOOST_ASSERT(this == cur.text());
1474 if (cur.lastpos() == 0) {
1475 // Paragraph is empty, so we just go to the right
1479 cur.selection() = true; // to avoid deletion
1482 // What is this test for ??? (JMarc)
1483 if (!cur.selection())
1484 deleteWordForward(cur);
1486 cutSelection(cur, true, false);
1491 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1493 BOOST_ASSERT(this == cur.text());
1497 if (cur.selection()) {
1498 from = cur.selBegin();
1502 getWord(from, to, lyx::PARTIAL_WORD);
1503 setCursor(cur, to.pit(), to.pos() + 1);
1506 recordUndoSelection(cur);
1508 pos_type pos = from.pos();
1509 int par = from.pit();
1511 while (par != int(pars_.size()) && (pos != to.pos() || par != to.pit())) {
1513 if (pos == pars_[pit].size()) {
1518 unsigned char c = pars_[pit].getChar(pos);
1519 if (c != Paragraph::META_INSET) {
1521 case text_lowercase:
1524 case text_capitalization:
1526 action = text_lowercase;
1528 case text_uppercase:
1533 #ifdef WITH_WARNINGS
1536 pars_[pit].setChar(pos, c);
1542 void LyXText::Delete(LCursor & cur)
1544 BOOST_ASSERT(this == cur.text());
1545 if (cur.pos() != cur.lastpos()) {
1546 recordUndo(cur, Undo::DELETE, cur.pit());
1547 setCursorIntern(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1550 // should we do anything in an else branch?
1554 void LyXText::backspace(LCursor & cur)
1556 BOOST_ASSERT(this == cur.text());
1557 if (cur.pos() == 0) {
1558 // The cursor is at the beginning of a paragraph, so
1559 // the the backspace will collapse two paragraphs into
1562 // but it's not allowed unless it's new
1563 Paragraph & par = cur.paragraph();
1564 if (par.isChangeEdited(0, par.size()))
1567 // we may paste some paragraphs
1569 // is it an empty paragraph?
1570 pos_type lastpos = cur.lastpos();
1571 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1572 // This is an empty paragraph and we delete it just
1573 // by moving the cursor one step
1574 // left and let the DeleteEmptyParagraphMechanism
1575 // handle the actual deletion of the paragraph.
1577 if (cur.pit() != 0) {
1584 recordUndo(cur, Undo::DELETE, cur.pit() - 1);
1586 pit_type tmppit = cur.pit();
1587 // We used to do cursorLeftIntern() here, but it is
1588 // not a good idea since it triggers the auto-delete
1589 // mechanism. So we do a cursorLeftIntern()-lite,
1590 // without the dreaded mechanism. (JMarc)
1591 if (cur.pit() != 0) {
1592 // steps into the above paragraph.
1593 setCursorIntern(cur, cur.pit() - 1,
1594 pars_[cur.pit() - 1].size(),
1598 // Pasting is not allowed, if the paragraphs have different
1599 // layout. I think it is a real bug of all other
1600 // word processors to allow it. It confuses the user.
1601 // Correction: Pasting is always allowed with standard-layout
1602 Buffer & buf = cur.buffer();
1603 BufferParams const & bufparams = buf.params();
1604 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1605 pit_type const cpit = cur.pit();
1608 && (pars_[cpit].layout() == pars_[tmppit].layout()
1609 || pars_[tmppit].layout() == tclass.defaultLayout())
1610 && pars_[cpit].getAlign() == pars_[tmppit].getAlign()) {
1611 mergeParagraph(bufparams, pars_, cpit);
1613 if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
1616 // the counters may have changed
1618 setCursor(cur, cur.pit(), cur.pos(), false);
1621 // this is the code for a normal backspace, not pasting
1623 recordUndo(cur, Undo::DELETE);
1624 // We used to do cursorLeftIntern() here, but it is
1625 // not a good idea since it triggers the auto-delete
1626 // mechanism. So we do a cursorLeftIntern()-lite,
1627 // without the dreaded mechanism. (JMarc)
1628 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1629 false, cur.boundary());
1630 cur.paragraph().erase(cur.pos());
1633 if (cur.pos() == cur.lastpos())
1634 setCurrentFont(cur);
1636 setCursor(cur, cur.pit(), cur.pos(), false, cur.boundary());
1640 Paragraph & LyXText::getPar(pit_type par) const
1642 //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
1643 BOOST_ASSERT(par >= 0);
1644 BOOST_ASSERT(par < int(paragraphs().size()));
1645 return paragraphs()[par];
1649 Row const & LyXText::firstRow() const
1651 return *paragraphs().front().rows().begin();
1655 void LyXText::redoParagraph(pit_type const pit)
1657 // remove rows of paragraph, keep track of height changes
1658 Paragraph & par = pars_[pit];
1661 InsetList::iterator ii = par.insetlist.begin();
1662 InsetList::iterator iend = par.insetlist.end();
1663 for (; ii != iend; ++ii) {
1665 int const w = maxwidth_ - leftMargin(pit) - rightMargin(par);
1666 MetricsInfo mi(bv(), getFont(par, ii->pos), w);
1667 ii->inset->metrics(mi, dim);
1670 // rebreak the paragraph
1674 par.setBeginOfBody();
1678 rowBreakPoint(pit, row);
1679 setRowWidth(pit, row);
1680 setHeightOfRow(pit, row);
1681 par.rows().push_back(row);
1682 dim.wid = std::max(dim.wid, row.width());
1683 dim.des += row.height();
1685 } while (z < par.size());
1687 dim.asc += par.rows()[0].ascent();
1688 dim.des -= par.rows()[0].ascent();
1690 //lyxerr << "redoParagraph: " << par.rows().size() << " rows\n";
1694 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1696 //BOOST_ASSERT(mi.base.textwidth);
1697 if (mi.base.textwidth)
1698 maxwidth_ = mi.base.textwidth;
1699 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1700 // << " maxWidth: " << maxwidth_ << "\nfont: " << mi.base.font << endl;
1704 for (pit_type pit = 0, n = paragraphs().size(); pit != n; ++pit) {
1706 Paragraph & par = paragraphs()[pit];
1708 if (w < par.width())
1713 dim.asc = pars_[0].ascent();
1714 dim.des = h - dim.asc;
1720 // only used for inset right now. should also be used for main text
1721 void LyXText::draw(PainterInfo & pi, int x, int y) const
1723 paintTextInset(*this, pi, x, y);
1728 // only used for inset right now. should also be used for main text
1729 void LyXText::drawSelection(PainterInfo & pi, int x , int) const
1731 LCursor & cur = pi.base.bv->cursor();
1732 if (!cur.selection())
1734 if (!ptr_cmp(cur.text(), this))
1737 lyxerr << "draw selection at " << x << endl;
1739 // is there a better way of getting these two iterators?
1740 DocIterator beg = cur;
1741 DocIterator end = cur;
1743 beg.top() = cur.selBegin();
1744 end.top() = cur.selEnd();
1746 // the selection doesn't touch the visible screen
1747 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
1748 || bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_ABOVE)
1751 Paragraph const & par1 = pars_[beg.pit()];
1752 Paragraph const & par2 = pars_[end.pit()];
1754 Row const & row1 = par1.getRow(beg.pos());
1755 Row const & row2 = par2.getRow(end.pos());
1758 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_ABOVE) {
1763 y1 = bv_funcs::getPos(beg).y_ - row1.ascent();
1764 int const startx = cursorX(beg.top());
1765 x1 = isRTL(par1) ? startx : 0;
1766 x2 = isRTL(par1) ? 0 + dim_.wid : startx;
1770 if (bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_BELOW) {
1771 y2 = pi.base.bv->workHeight();
1775 y2 = bv_funcs::getPos(end).y_ + row2.descent();
1776 int const endx = cursorX(end.top());
1777 X1 = isRTL(par2) ? 0 : endx;
1778 X2 = isRTL(par2) ? endx : 0 + dim_.wid;
1781 lyxerr << " y1: " << y1 << " y2: " << y2
1782 << " xo: " << xo_ << " wid: " << dim_.wid
1785 // paint big rectangle in one go
1786 pi.pain.fillRectangle(x, y1, dim_.wid, y2 - y1, LColor::selection);
1788 // reset background at begin of first selected line
1789 pi.pain.fillRectangle(x + x1, y1, x2 - x1, row1.height(),
1790 LColor::background);
1792 // reset background at end of last selected line
1793 pi.pain.fillRectangle(x + X1, y2 - row2.height(),
1794 X2 - X1, row2.height(), LColor::background);
1799 void LyXText::drawSelection(PainterInfo & pi, int x, int) const
1801 LCursor & cur = pi.base.bv->cursor();
1802 if (!cur.selection())
1804 if (!ptr_cmp(cur.text(), this))
1807 lyxerr << "draw selection at " << x << endl;
1809 // is there a better way of getting these two iterators?
1810 DocIterator beg = cur;
1811 DocIterator end = cur;
1813 beg.top() = cur.selBegin();
1814 end.top() = cur.selEnd();
1816 // the selection doesn't touch the visible screen
1817 if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
1818 || bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_ABOVE)
1821 Paragraph const & par1 = pars_[beg.pit()];
1822 Paragraph const & par2 = pars_[end.pit()];
1824 bool const above = (bv_funcs::status(pi.base.bv, beg)
1825 == bv_funcs::CUR_ABOVE);
1826 bool const below = (bv_funcs::status(pi.base.bv, end)
1827 == bv_funcs::CUR_BELOW);
1835 Row const & row1 = par1.getRow(beg.pos());
1836 y1 = bv_funcs::getPos(beg).y_ - row1.ascent();
1837 y2 = y1 + row1.height();
1838 int const startx = cursorX(beg.top());
1839 x1 = !isRTL(par1) ? startx : 0;
1840 x2 = !isRTL(par1) ? 0 + dim_.wid : startx;
1845 Y1 = pi.base.bv->workHeight();
1846 Y2 = pi.base.bv->workHeight();
1850 Row const & row2 = par2.getRow(end.pos());
1851 Y1 = bv_funcs::getPos(end).y_ - row2.ascent();
1852 Y2 = Y1 + row2.height();
1853 int const endx = cursorX(end.top());
1854 X1 = !isRTL(par2) ? 0 : endx;
1855 X2 = !isRTL(par2) ? endx : 0 + dim_.wid;
1858 if (!above && !below && &par1.getRow(beg.pos())
1859 == &par2.getRow(end.pos()))
1861 // paint only one rectangle
1862 pi.pain.fillRectangle(x + x1, y1, X2 - x1, y2 - y1,
1867 // paint upper rectangle
1868 pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
1870 // paint bottom rectangle
1871 pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
1873 // paint center rectangle
1874 pi.pain.fillRectangle(x, y2, dim_.wid,
1875 Y1 - y2, LColor::selection);
1878 bool LyXText::isLastRow(pit_type pit, Row const & row) const
1880 return row.endpos() >= pars_[pit].size()
1881 && pit + 1 == pit_type(paragraphs().size());
1885 bool LyXText::isFirstRow(pit_type pit, Row const & row) const
1887 return row.pos() == 0 && pit == 0;
1891 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1892 word_location const loc)
1894 Paragraph const & from_par = pars_[from.pit()];
1896 case lyx::WHOLE_WORD_STRICT:
1897 if (from.pos() == 0 || from.pos() == from_par.size()
1898 || !from_par.isLetter(from.pos())
1899 || !from_par.isLetter(from.pos() - 1)) {
1903 // no break here, we go to the next
1905 case lyx::WHOLE_WORD:
1906 // If we are already at the beginning of a word, do nothing
1907 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
1909 // no break here, we go to the next
1911 case lyx::PREVIOUS_WORD:
1912 // always move the cursor to the beginning of previous word
1913 while (from.pos() && from_par.isLetter(from.pos() - 1))
1916 case lyx::NEXT_WORD:
1917 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1920 case lyx::PARTIAL_WORD:
1921 // no need to move the 'from' cursor
1925 Paragraph & to_par = pars_[to.pit()];
1926 while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
1931 void LyXText::write(Buffer const & buf, std::ostream & os) const
1933 ParagraphList::const_iterator pit = paragraphs().begin();
1934 ParagraphList::const_iterator end = paragraphs().end();
1935 Paragraph::depth_type dth = 0;
1936 for (; pit != end; ++pit)
1937 pit->write(buf, os, buf.params(), dth);
1941 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1943 static Change current_change;
1945 Paragraph::depth_type depth = 0;
1947 while (lex.isOK()) {
1949 string const token = lex.getString();
1954 if (token == "\\end_inset") {
1958 if (token == "\\end_body") {
1962 if (token == "\\begin_body") {
1966 if (token == "\\end_document") {
1970 if (token == "\\begin_layout") {
1971 lex.pushToken(token);
1974 par.params().depth(depth);
1975 if (buf.params().tracking_changes)
1977 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1978 pars_.push_back(par);
1980 // FIXME: goddamn InsetTabular makes us pass a Buffer
1982 ::readParagraph(buf, pars_.back(), lex);
1984 } else if (token == "\\begin_deeper") {
1986 } else if (token == "\\end_deeper") {
1988 lex.printError("\\end_deeper: " "depth is already null");
1993 lyxerr << "Handling unknown body token: `"
1994 << token << '\'' << endl;
2001 int LyXText::ascent() const
2007 int LyXText::descent() const
2013 int LyXText::cursorX(CursorSlice const & cur) const
2015 pit_type const pit = cur.pit();
2016 Paragraph const & par = pars_[pit];
2017 if (par.rows().empty())
2020 Row const & row = par.getRow(cur.pos());
2022 pos_type pos = cur.pos();
2023 pos_type cursor_vpos = 0;
2025 RowMetrics const m = computeRowMetrics(pit, row);
2028 pos_type const row_pos = row.pos();
2029 pos_type const end = row.endpos();
2032 cursor_vpos = row_pos;
2033 else if (pos >= end)
2034 cursor_vpos = isRTL(par) ? row_pos : end;
2035 else if (pos > row_pos && pos >= end)
2036 // Place cursor after char at (logical) position pos - 1
2037 cursor_vpos = (bidi.level(pos - 1) % 2 == 0)
2038 ? bidi.log2vis(pos - 1) + 1 : bidi.log2vis(pos - 1);
2040 // Place cursor before char at (logical) position pos
2041 cursor_vpos = (bidi.level(pos) % 2 == 0)
2042 ? bidi.log2vis(pos) : bidi.log2vis(pos) + 1;
2044 pos_type body_pos = par.beginOfBody();
2046 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
2049 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
2050 pos_type pos = bidi.vis2log(vpos);
2051 if (body_pos > 0 && pos == body_pos - 1) {
2053 + font_metrics::width(par.layout()->labelsep,
2055 if (par.isLineSeparator(body_pos - 1))
2056 x -= singleWidth(par, body_pos - 1);
2059 if (hfillExpansion(par, row, pos)) {
2060 x += singleWidth(par, pos);
2061 if (pos >= body_pos)
2065 } else if (par.isSeparator(pos)) {
2066 x += singleWidth(par, pos);
2067 if (pos >= body_pos)
2070 x += singleWidth(par, pos);
2076 int LyXText::cursorY(CursorSlice const & cur) const
2078 Paragraph const & par = getPar(cur.pit());
2080 h -= pars_[0].rows()[0].ascent();
2081 for (pit_type pit = 0; pit < cur.pit(); ++pit)
2082 h += pars_[pit].height();
2083 for (size_t rit = 0, rend = par.pos2row(cur.pos()); rit != rend; ++rit)
2084 h += par.rows()[rit].height();
2085 h += par.rows()[par.pos2row(cur.pos())].ascent();
2090 // Returns the current font and depth as a message.
2091 string LyXText::currentState(LCursor & cur)
2093 BOOST_ASSERT(this == cur.text());
2094 Buffer & buf = cur.buffer();
2095 Paragraph const & par = cur.paragraph();
2096 std::ostringstream os;
2098 bool const show_change = buf.params().tracking_changes
2099 && cur.pos() != cur.lastpos()
2100 && par.lookupChange(cur.pos()) != Change::UNCHANGED;
2103 Change change = par.lookupChangeFull(cur.pos());
2104 Author const & a = buf.params().authors().get(change.author);
2105 os << _("Change: ") << a.name();
2106 if (!a.email().empty())
2107 os << " (" << a.email() << ")";
2108 if (change.changetime)
2109 os << _(" at ") << ctime(&change.changetime);
2113 // I think we should only show changes from the default
2115 LyXFont font = real_current_font;
2116 font.reduce(buf.params().getLyXTextClass().defaultfont());
2118 // avoid _(...) re-entrance problem
2119 string const s = font.stateText(&buf.params());
2120 os << bformat(_("Font: %1$s"), s);
2122 // os << bformat(_("Font: %1$s"), font.stateText(&buf.params));
2124 // The paragraph depth
2125 int depth = cur.paragraph().getDepth();
2127 os << bformat(_(", Depth: %1$d"), depth);
2129 // The paragraph spacing, but only if different from
2131 Spacing const & spacing = par.params().spacing();
2132 if (!spacing.isDefault()) {
2133 os << _(", Spacing: ");
2134 switch (spacing.getSpace()) {
2135 case Spacing::Single:
2138 case Spacing::Onehalf:
2141 case Spacing::Double:
2144 case Spacing::Other:
2145 os << _("Other (") << spacing.getValueAsString() << ')';
2147 case Spacing::Default:
2148 // should never happen, do nothing
2153 #ifdef DEVEL_VERSION
2154 os << _(", Inset: ") << &cur.inset();
2155 os << _(", Paragraph: ") << cur.pit();
2156 os << _(", Id: ") << par.id();
2157 os << _(", Position: ") << cur.pos();
2158 // Row & row = cur.textRow();
2159 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2165 string LyXText::getPossibleLabel(LCursor & cur) const
2167 pit_type pit = cur.pit();
2169 LyXLayout_ptr layout = pars_[pit].layout();
2171 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2172 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
2173 if (layout2->latextype != LATEX_PARAGRAPH) {
2179 string text = layout->latexname().substr(0, 3);
2180 if (layout->latexname() == "theorem")
2181 text = "thm"; // Create a correct prefix for prettyref
2184 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2187 string par_text = pars_[pit].asString(cur.buffer(), false);
2188 for (int i = 0; i < lyxrc.label_init_length; ++i) {
2189 if (par_text.empty())
2192 par_text = split(par_text, head, ' ');
2193 // Is it legal to use spaces in labels ?
2203 //pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2207 // Paragraph const & par = pars_[pit];
2208 // Row const & r = par.rows()[row];
2209 // int pos = r.pos();
2210 // for (; currx < x && pos < r.endpos(); ++pos) {
2212 // currx += singleWidth(par, pos);
2214 // if (abs(lastx - x) < abs(currx - x) && pos != r.pos())
2220 pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2223 Row const & r = pars_[pit].rows()[row];
2224 return r.pos() + getColumnNearX(pit, r, x, bound);
2228 //int LyXText::pos2x(pit_type pit, pos_type pos) const
2230 // Paragraph const & par = pars_[pit];
2231 // Row const & r = par.rows()[row];
2237 // x,y are screen coordinates
2238 // sets cursor only within this LyXText
2239 void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
2241 pit_type pit = getPitNearY(y);
2242 int yy = theCoords.get(this, pit).y_ - pars_[pit].ascent();
2243 lyxerr << "setCursorFromCoordinates: x: " << x << " y: " << y
2244 << " pit: " << pit << " yy: " << yy << endl;
2246 Paragraph const & par = pars_[pit];
2248 BOOST_ASSERT(par.rows().size());
2249 for (; r < int(par.rows().size()) - 1; ++r) {
2250 Row const & row = par.rows()[r];
2251 if (int(yy + row.height()) > y)
2256 Row const & row = par.rows()[r];
2258 lyxerr << "setCursorFromCoordinates: row " << r
2259 << " from pos: " << row.pos() << endl;
2263 pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
2264 setCursor(cur, pit, pos, true, bound);