3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetText.h"
15 #include "insets/InsetArgument.h"
16 #include "insets/InsetLayout.h"
18 #include "buffer_funcs.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "CompletionList.h"
23 #include "CoordCache.h"
25 #include "CutAndPaste.h"
26 #include "DispatchResult.h"
27 #include "ErrorList.h"
28 #include "FuncRequest.h"
29 #include "FuncStatus.h"
30 #include "InsetCaption.h"
31 #include "InsetList.h"
35 #include "LaTeXFeatures.h"
39 #include "MetricsInfo.h"
40 #include "output_docbook.h"
41 #include "output_latex.h"
42 #include "output_xhtml.h"
43 #include "OutputParams.h"
44 #include "output_plaintext.h"
45 #include "Paragraph.h"
46 #include "ParagraphParameters.h"
47 #include "ParIterator.h"
51 #include "texstream.h"
52 #include "TextClass.h"
54 #include "TextMetrics.h"
55 #include "TocBackend.h"
57 #include "frontends/alert.h"
58 #include "frontends/Painter.h"
60 #include "support/bind.h"
61 #include "support/convert.h"
62 #include "support/debug.h"
63 #include "support/gettext.h"
64 #include "support/lassert.h"
65 #include "support/lstrings.h"
66 #include "support/RefChanger.h"
72 using namespace lyx::support;
77 using graphics::PreviewLoader;
80 /////////////////////////////////////////////////////////////////////
82 InsetText::InsetText(Buffer * buf, UsePlain type)
83 : Inset(buf), drawFrame_(false), frame_color_(Color_insetframe),
84 text_(this, type == DefaultLayout)
89 InsetText::InsetText(InsetText const & in)
90 : Inset(in), text_(this, in.text_)
92 drawFrame_ = in.drawFrame_;
93 frame_color_ = in.frame_color_;
97 void InsetText::setBuffer(Buffer & buf)
99 ParagraphList::iterator end = paragraphs().end();
100 for (ParagraphList::iterator it = paragraphs().begin(); it != end; ++it)
102 Inset::setBuffer(buf);
106 void InsetText::setMacrocontextPositionRecursive(DocIterator const & pos)
108 text_.setMacrocontextPosition(pos);
110 ParagraphList::const_iterator pit = paragraphs().begin();
111 ParagraphList::const_iterator pend = paragraphs().end();
112 for (; pit != pend; ++pit) {
113 InsetList::const_iterator iit = pit->insetList().begin();
114 InsetList::const_iterator end = pit->insetList().end();
115 for (; iit != end; ++iit) {
116 if (InsetText * txt = iit->inset->asInsetText()) {
117 DocIterator ppos(pos);
118 ppos.push_back(CursorSlice(*txt));
119 iit->inset->asInsetText()->setMacrocontextPositionRecursive(ppos);
126 void InsetText::clear()
128 ParagraphList & pars = paragraphs();
129 LBUFERR(!pars.empty());
131 // This is a gross hack...
132 Layout const & old_layout = pars.begin()->layout();
135 pars.push_back(Paragraph());
136 pars.begin()->setInsetOwner(this);
137 pars.begin()->setLayout(old_layout);
141 Dimension const InsetText::dimension(BufferView const & bv) const
143 TextMetrics const & tm = bv.textMetrics(&text_);
144 Dimension dim = tm.dimension();
145 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
146 dim.des += TEXT_TO_INSET_OFFSET;
147 dim.asc += TEXT_TO_INSET_OFFSET;
152 void InsetText::write(ostream & os) const
159 void InsetText::read(Lexer & lex)
163 // delete the initial paragraph
164 Paragraph oldpar = *paragraphs().begin();
165 paragraphs().clear();
167 lex.setContext("InsetText::read");
168 bool res = text_.read(lex, errorList, this);
171 lex.printError("Missing \\end_inset at this point. ");
174 // ensure we have at least one paragraph.
175 if (paragraphs().empty())
176 paragraphs().push_back(oldpar);
177 // Force default font, if so requested
178 // This avoids paragraphs in buffer language that would have a
179 // foreign language after a document language change, and it ensures
180 // that all new text in ERT and similar gets the "latex" language,
181 // since new text inherits the language from the last position of the
182 // existing text. As a side effect this makes us also robust against
183 // bugs in LyX that might lead to font changes in ERT in .lyx files.
188 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
190 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
192 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
194 // Hand font through to contained lyxtext:
195 tm.font_.fontInfo() = mi.base.font;
196 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
198 // This can happen when a layout has a left and right margin,
199 // and the view is made very narrow. We can't do better than
200 // to draw it partly out of view (bug 5890).
201 if (mi.base.textwidth < 1)
202 mi.base.textwidth = 1;
205 tm.metrics(mi, dim, mi.base.textwidth);
208 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
209 dim.asc += TEXT_TO_INSET_OFFSET;
210 dim.des += TEXT_TO_INSET_OFFSET;
211 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
215 void InsetText::draw(PainterInfo & pi, int x, int y) const
217 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
219 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
220 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
221 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
222 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
223 bool change_drawn = false;
224 if (drawFrame_ || pi.full_repaint) {
226 pi.pain.fillRectangle(xframe, yframe, w, h,
227 pi.backgroundColor(this));
229 // Change color of the frame in tracked changes, like for tabulars.
230 // Only do so if the color is not custom. But do so even if RowPainter
231 // handles the strike-through already.
233 if (pi.change_.changed()
234 // Originally, these are the colors with role Text, from role() in
235 // ColorCache.cpp. The code is duplicated to avoid depending on Qt
236 // types, and also maybe it need not match in the future.
237 && (frameColor() == Color_foreground
238 || frameColor() == Color_cursor
239 || frameColor() == Color_preview
240 || frameColor() == Color_tabularline
241 || frameColor() == Color_previewframe)) {
242 c = pi.change_.color();
247 pi.pain.rectangle(xframe, yframe, w, h, c);
250 Changer dummy = make_change(pi.background_color,
251 pi.backgroundColor(this, false));
252 // The change tracking cue must not be inherited
253 Changer dummy2 = make_change(pi.change_, Change());
254 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
256 if (canPaintChange(*pi.base.bv) && (!change_drawn || pi.change_.deleted()))
257 // Do not draw the change tracking cue if already done by RowPainter and
258 // do not draw the cue for INSERTED if the information is already in the
259 // color of the frame
260 pi.change_.paintCue(pi, xframe, yframe, xframe + w, yframe + h);
264 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
266 pit_type const pit = front ? 0 : paragraphs().size() - 1;
267 pos_type pos = front ? 0 : paragraphs().back().size();
269 // if visual information is not to be ignored, move to extreme right/left
270 if (entry_from != ENTRY_DIRECTION_IGNORE) {
271 Cursor temp_cur = cur;
272 temp_cur.pit() = pit;
273 temp_cur.pos() = pos;
274 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
275 pos = temp_cur.pos();
278 cur.top().setPitPos(pit, pos);
283 Inset * InsetText::editXY(Cursor & cur, int x, int y)
285 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
289 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
291 LYXERR(Debug::ACTION, "InsetText::doDispatch(): cmd: " << cmd);
293 // See bug #9042, for instance.
294 if (isPassThru() && lyxCode() != ARG_CODE) {
295 // Force any new text to latex_language FIXME: This
296 // should only be necessary in constructor, but new
297 // paragraphs that are created by pressing enter at
298 // the start of an existing paragraph get the buffer
299 // language and not latex_language, so we take this
300 // brute force approach.
301 cur.current_font.setLanguage(latex_language);
302 cur.real_current_font.setLanguage(latex_language);
305 switch (cmd.action()) {
307 case LFUN_CLIPBOARD_PASTE:
308 case LFUN_SELECTION_PASTE:
309 case LFUN_PRIMARY_SELECTION_PASTE:
310 text_.dispatch(cur, cmd);
311 // If we we can only store plain text, we must reset all
313 // FIXME: Change only the pasted paragraphs
317 case LFUN_INSET_DISSOLVE: {
318 bool const main_inset = text_.isMainText();
319 bool const target_inset = cmd.argument().empty()
320 || cmd.getArg(0) == insetName(lyxCode());
321 // cur.inset() is the tabular when this is a single cell (bug #9954)
322 bool const one_cell = cur.inset().nargs() == 1;
324 if (!main_inset && target_inset && one_cell) {
325 // Text::dissolveInset assumes that the cursor
326 // is inside the Inset.
327 if (&cur.inset() != this)
328 cur.pushBackward(*this);
329 cur.beginUndoGroup();
330 text_.dispatch(cur, cmd);
338 text_.dispatch(cur, cmd);
341 if (!cur.result().dispatched())
342 Inset::doDispatch(cur, cmd);
346 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
347 FuncStatus & status) const
349 switch (cmd.action()) {
350 case LFUN_INSET_DISSOLVE: {
351 bool const main_inset = text_.isMainText();
352 bool const target_inset = cmd.argument().empty()
353 || cmd.getArg(0) == insetName(lyxCode());
354 // cur.inset() is the tabular when this is a single cell (bug #9954)
355 bool const one_cell = cur.inset().nargs() == 1;
358 status.setEnabled(!main_inset && one_cell);
362 case LFUN_ARGUMENT_INSERT: {
363 string const arg = cmd.getArg(0);
365 status.setEnabled(false);
368 if (text_.isMainText() || !cur.paragraph().layout().args().empty())
369 return text_.getStatus(cur, cmd, status);
371 Layout::LaTeXArgMap args = getLayout().args();
372 Layout::LaTeXArgMap::const_iterator const lait = args.find(arg);
373 if (lait != args.end()) {
374 status.setEnabled(true);
375 ParagraphList::const_iterator pit = paragraphs().begin();
376 for (; pit != paragraphs().end(); ++pit) {
377 InsetList::const_iterator it = pit->insetList().begin();
378 InsetList::const_iterator end = pit->insetList().end();
379 for (; it != end; ++it) {
380 if (it->inset->lyxCode() == ARG_CODE) {
381 InsetArgument const * ins =
382 static_cast<InsetArgument const *>(it->inset);
383 if (ins->name() == arg) {
384 // we have this already
385 status.setEnabled(false);
392 status.setEnabled(false);
397 // Dispatch only to text_ if the cursor is inside
398 // the text_. It is not for context menus (bug 5797).
400 if (cur.text() == &text_)
401 ret = text_.getStatus(cur, cmd, status);
404 ret = Inset::getStatus(cur, cmd, status);
410 void InsetText::fixParagraphsFont()
412 Font font(inherit_font, buffer().params().language);
413 font.setLanguage(latex_language);
414 ParagraphList::iterator par = paragraphs().begin();
415 ParagraphList::iterator const end = paragraphs().end();
417 if (par->isPassThru())
418 par->resetFonts(font);
419 if (!par->allowParagraphCustomization())
420 par->params().clear();
426 void InsetText::setChange(Change const & change)
428 ParagraphList::iterator pit = paragraphs().begin();
429 ParagraphList::iterator end = paragraphs().end();
430 for (; pit != end; ++pit) {
431 pit->setChange(change);
436 void InsetText::acceptChanges()
438 text_.acceptChanges();
442 void InsetText::rejectChanges()
444 text_.rejectChanges();
448 void InsetText::validate(LaTeXFeatures & features) const
450 features.useInsetLayout(getLayout());
451 for (Paragraph const & p : paragraphs())
452 p.validate(features);
456 void InsetText::latex(otexstream & os, OutputParams const & runparams) const
458 // This implements the standard way of handling the LaTeX
459 // output of a text inset, either a command or an
460 // environment. Standard collapsable insets should not
461 // redefine this, non-standard ones may call this.
462 InsetLayout const & il = getLayout();
463 if (il.forceOwnlines())
465 if (!il.latexname().empty()) {
466 if (il.latextype() == InsetLayout::COMMAND) {
468 // FIXME \protect should only be used for fragile
469 // commands, but we do not provide this information yet.
470 if (runparams.moving_arg)
472 os << '\\' << from_utf8(il.latexname());
473 if (!il.latexargs().empty())
474 getArgs(os, runparams);
475 if (!il.latexparam().empty())
476 os << from_utf8(il.latexparam());
478 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
483 if (runparams.lastid != -1)
484 os.texrow().start(runparams.lastid,
486 os << "\\begin{" << from_utf8(il.latexname()) << "}";
487 if (!il.latexargs().empty())
488 getArgs(os, runparams);
489 if (!il.latexparam().empty())
490 os << from_utf8(il.latexparam());
494 if (!il.latexargs().empty())
495 getArgs(os, runparams);
496 if (!il.latexparam().empty())
497 os << from_utf8(il.latexparam());
500 if (!il.leftdelim().empty())
501 os << il.leftdelim();
503 OutputParams rp = runparams;
506 if (il.isNeedProtect())
507 rp.moving_arg = true;
508 if (!il.passThruChars().empty())
509 rp.pass_thru_chars += il.passThruChars();
511 rp.par_end = paragraphs().size();
513 // Output the contents of the inset
514 latexParagraphs(buffer(), text_, os, rp);
515 runparams.encoding = rp.encoding;
517 if (!il.rightdelim().empty())
518 os << il.rightdelim();
520 if (!il.latexname().empty()) {
521 if (il.latextype() == InsetLayout::COMMAND) {
523 if (!il.postcommandargs().empty())
524 getArgs(os, runparams, true);
525 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
526 // A comment environment doesn't need a % before \n\end
527 if (il.isDisplay() || runparams.inComment)
531 os << "\\end{" << from_utf8(il.latexname()) << "}" << breakln;
533 os.protectSpace(true);
536 if (il.forceOwnlines())
541 int InsetText::plaintext(odocstringstream & os,
542 OutputParams const & runparams, size_t max_length) const
544 ParagraphList::const_iterator beg = paragraphs().begin();
545 ParagraphList::const_iterator end = paragraphs().end();
546 ParagraphList::const_iterator it = beg;
547 bool ref_printed = false;
549 for (; it != end; ++it) {
552 if (runparams.linelen > 0)
555 odocstringstream oss;
556 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed, max_length);
557 docstring const str = oss.str();
559 // FIXME: len is not computed fully correctly; in principle,
560 // we have to count the characters after the last '\n'
562 if (os.str().size() >= max_length)
570 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
572 ParagraphList::const_iterator const beg = paragraphs().begin();
575 sgml::openTag(os, getLayout().latexname(),
576 beg->getID(buffer(), runparams) + getLayout().latexparam());
578 docbookParagraphs(text_, buffer(), os, runparams);
581 sgml::closeTag(os, getLayout().latexname());
587 docstring InsetText::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
589 return insetAsXHTML(xs, runparams, WriteEverything);
594 // There are cases where we may need to close open fonts and such
595 // and then re-open them when we are done. This would be the case, e.g.,
596 // if we were otherwise about to write:
597 // <em>word <div class='foot'>footnote text.</div> emph</em>
598 // The problem isn't so much that the footnote text will get emphasized:
599 // we can handle that with CSS. The problem is that this is invalid XHTML.
600 // One solution would be to make the footnote <span>, but the problem is
601 // completely general, and so we'd have to make absolutely everything into
602 // span. What I think will work is to check if we're about to write "div" and,
603 // if so, try to close fonts, etc.
604 // There are probably limits to how well we can do here, though, and we will
605 // have to rely upon users not putting footnotes inside noun-type insets.
606 docstring InsetText::insetAsXHTML(XHTMLStream & xs, OutputParams const & rp,
607 XHTMLOptions opts) const
609 // we will always want to output all our paragraphs when we are
611 OutputParams runparams = rp;
612 runparams.par_begin = 0;
613 runparams.par_end = text().paragraphs().size();
616 xhtmlParagraphs(text_, buffer(), xs, runparams);
620 InsetLayout const & il = getLayout();
621 if (opts & WriteOuterTag)
622 xs << html::StartTag(il.htmltag(), il.htmlattr());
624 if ((opts & WriteLabel) && !il.counter().empty()) {
625 BufferParams const & bp = buffer().masterBuffer()->params();
626 Counters & cntrs = bp.documentClass().counters();
627 cntrs.step(il.counter(), OutputUpdate);
628 // FIXME: translate to paragraph language
629 if (!il.htmllabel().empty()) {
630 docstring const lbl =
631 cntrs.counterLabel(from_utf8(il.htmllabel()), bp.language->code());
632 // FIXME is this check necessary?
634 xs << html::StartTag(il.htmllabeltag(), il.htmllabelattr());
636 xs << html::EndTag(il.htmllabeltag());
641 if (opts & WriteInnerTag)
642 xs << html::StartTag(il.htmlinnertag(), il.htmlinnerattr());
644 // we will eventually lose information about the containing inset
645 if (!allowMultiPar() || opts == JustText)
646 runparams.html_make_pars = false;
648 runparams.pass_thru = true;
650 xhtmlParagraphs(text_, buffer(), xs, runparams);
652 if (opts & WriteInnerTag)
653 xs << html::EndTag(il.htmlinnertag());
655 if (opts & WriteOuterTag)
656 xs << html::EndTag(il.htmltag());
661 void InsetText::getArgs(otexstream & os, OutputParams const & runparams_in,
662 bool const post) const
664 OutputParams runparams = runparams_in;
665 runparams.local_font =
666 ¶graphs()[0].getFirstFontSettings(buffer().masterBuffer()->params());
668 runparams.pass_thru = true;
670 latexArgInsets(paragraphs(), paragraphs().begin(), os, runparams, getLayout().postcommandargs(), "post:");
672 latexArgInsets(paragraphs(), paragraphs().begin(), os, runparams, getLayout().latexargs());
676 void InsetText::cursorPos(BufferView const & bv,
677 CursorSlice const & sl, bool boundary, int & x, int & y) const
679 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
680 y = bv.textMetrics(&text_).cursorY(sl, boundary);
684 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
687 Paragraph & first = paragraphs().front();
688 for (unsigned int i = 0; i < data.length(); ++i)
689 first.insertChar(i, data[i], font, trackChanges);
693 void InsetText::setDrawFrame(bool flag)
699 ColorCode InsetText::frameColor() const
705 void InsetText::setFrameColor(ColorCode col)
711 void InsetText::appendParagraphs(ParagraphList & plist)
713 // There is little we can do here to keep track of changes.
714 // As of 2006/10/20, appendParagraphs is used exclusively by
715 // LyXTabular::setMultiColumn. In this context, the paragraph break
716 // is lost irreversibly and the appended text doesn't really change
718 ParagraphList & pl = paragraphs();
720 ParagraphList::iterator pit = plist.begin();
721 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
723 mergeParagraph(buffer().params(), pl,
724 distance(pl.begin(), ins) - 1);
726 for_each(pit, plist.end(),
727 bind(&ParagraphList::push_back, ref(pl), _1));
731 void InsetText::addPreview(DocIterator const & text_inset_pos,
732 PreviewLoader & loader) const
734 ParagraphList::const_iterator pit = paragraphs().begin();
735 ParagraphList::const_iterator pend = paragraphs().end();
738 DocIterator inset_pos = text_inset_pos;
739 inset_pos.push_back(CursorSlice(*const_cast<InsetText *>(this)));
741 for (; pit != pend; ++pit, ++pidx) {
742 InsetList::const_iterator it = pit->insetList().begin();
743 InsetList::const_iterator end = pit->insetList().end();
744 inset_pos.pit() = pidx;
745 for (; it != end; ++it) {
746 inset_pos.pos() = it->pos;
747 it->inset->addPreview(inset_pos, loader);
753 ParagraphList const & InsetText::paragraphs() const
755 return text_.paragraphs();
759 ParagraphList & InsetText::paragraphs()
761 return text_.paragraphs();
765 bool InsetText::insetAllowed(InsetCode code) const
768 // Arguments are also allowed in PassThru insets
772 return !isPassThru();
777 void InsetText::updateBuffer(ParIterator const & it, UpdateType utype)
779 ParIterator it2 = it;
781 LASSERT(&it2.inset() == this && it2.pit() == 0, return);
782 if (producesOutput()) {
783 InsetLayout const & il = getLayout();
784 bool const save_layouts = utype == OutputUpdate && il.htmlisblock();
785 Counters & cnt = buffer().masterBuffer()->params().documentClass().counters();
787 // LYXERR0("Entering " << name());
788 cnt.clearLastLayout();
789 // FIXME cnt.saveLastCounter()?
791 buffer().updateBuffer(it2, utype);
793 // LYXERR0("Exiting " << name());
794 cnt.restoreLastLayout();
795 // FIXME cnt.restoreLastCounter()?
798 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
799 // Note that we do not need to call:
800 // tclass.counters().clearLastLayout()
801 // since we are saving and restoring the existing counters, etc.
802 Counters const savecnt = tclass.counters();
803 tclass.counters().reset();
804 // we need float information even in note insets (#9760)
805 tclass.counters().current_float(savecnt.current_float());
806 tclass.counters().isSubfloat(savecnt.isSubfloat());
807 buffer().updateBuffer(it2, utype);
808 tclass.counters() = savecnt;
813 void InsetText::toString(odocstream & os) const
815 os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
819 void InsetText::forOutliner(docstring & os, size_t const maxlen,
820 bool const shorten) const
822 if (!getLayout().isInToc())
824 text().forOutliner(os, maxlen, shorten);
828 void InsetText::addToToc(DocIterator const & cdit, bool output_active,
829 UpdateType utype) const
831 DocIterator dit = cdit;
832 dit.push_back(CursorSlice(const_cast<InsetText &>(*this)));
833 iterateForToc(dit, output_active, utype);
837 void InsetText::iterateForToc(DocIterator const & cdit, bool output_active,
838 UpdateType utype) const
840 DocIterator dit = cdit;
841 // This also ensures that any document has a table of contents
842 shared_ptr<Toc> toc = buffer().tocBackend().toc("tableofcontents");
844 BufferParams const & bufparams = buffer_->params();
845 int const min_toclevel = bufparams.documentClass().min_toclevel();
846 // we really should have done this before we got here, but it
847 // can't hurt too much to do it again
848 bool const doing_output = output_active && producesOutput();
850 // For each paragraph, traverse its insets and let them add
852 ParagraphList const & pars = paragraphs();
853 pit_type pend = paragraphs().size();
854 for (pit_type pit = 0; pit != pend; ++pit) {
855 Paragraph const & par = pars[pit];
857 // if we find an optarg, we'll save it for use later.
858 InsetText const * arginset = 0;
859 InsetList::const_iterator it = par.insetList().begin();
860 InsetList::const_iterator end = par.insetList().end();
861 for (; it != end; ++it) {
862 Inset & inset = *it->inset;
864 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
865 inset.addToToc(dit, doing_output, utype);
866 if (inset.lyxCode() == ARG_CODE)
867 arginset = inset.asInsetText();
869 // now the toc entry for the paragraph
870 int const toclevel = text().getTocLevel(pit);
871 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
872 // insert this into the table of contents
874 int const length = (doing_output && utype == OutputUpdate) ?
875 INT_MAX : TOC_ENTRY_LENGTH;
877 tocstring = par.labelString();
878 if (!tocstring.empty())
880 arginset->text().forOutliner(tocstring, length);
882 par.forOutliner(tocstring, length);
884 toc->push_back(TocItem(dit, toclevel - min_toclevel,
885 tocstring, doing_output));
888 // And now the list of changes.
889 par.addChangesToToc(dit, buffer(), doing_output);
894 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
896 if (buffer().isClean())
897 return Inset::notifyCursorLeaves(old, cur);
899 // find text inset in old cursor
900 Cursor insetCur = old;
901 int scriptSlice = insetCur.find(this);
902 // we can try to continue here. returning true means
903 // the cursor is "now" invalid. which it was.
904 LASSERT(scriptSlice != -1, return true);
905 insetCur.cutOff(scriptSlice);
906 LASSERT(&insetCur.inset() == this, return true);
908 // update the old paragraph's words
909 insetCur.paragraph().updateWords();
911 return Inset::notifyCursorLeaves(old, cur);
915 bool InsetText::completionSupported(Cursor const & cur) const
917 //LASSERT(&cur.bv().cursor().inset() == this, return false);
918 return text_.completionSupported(cur);
922 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
924 return completionSupported(cur);
928 bool InsetText::automaticInlineCompletion() const
930 return lyxrc.completion_inline_text;
934 bool InsetText::automaticPopupCompletion() const
936 return lyxrc.completion_popup_text;
940 bool InsetText::showCompletionCursor() const
942 return lyxrc.completion_cursor_text;
946 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
948 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
952 docstring InsetText::completionPrefix(Cursor const & cur) const
954 if (!completionSupported(cur))
956 return text_.completionPrefix(cur);
960 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
963 if (!completionSupported(cur))
966 return text_.insertCompletion(cur, s, finished);
970 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
971 Dimension & dim) const
973 TextMetrics const & tm = cur.bv().textMetrics(&text_);
974 tm.completionPosAndDim(cur, x, y, dim);
978 string InsetText::contextMenu(BufferView const &, int, int) const
980 string context_menu = contextMenuName();
981 if (context_menu != InsetText::contextMenuName())
982 context_menu += ";" + InsetText::contextMenuName();
987 string InsetText::contextMenuName() const
989 return "context-edit";
993 docstring InsetText::toolTipText(docstring prefix, size_t const len) const
995 OutputParams rp(&buffer().params().encoding());
996 rp.for_tooltip = true;
997 odocstringstream oss;
1000 ParagraphList::const_iterator beg = paragraphs().begin();
1001 ParagraphList::const_iterator end = paragraphs().end();
1002 ParagraphList::const_iterator it = beg;
1003 bool ref_printed = false;
1005 for (; it != end; ++it) {
1008 writePlaintextParagraph(buffer(), *it, oss, rp, ref_printed, len);
1009 if (oss.tellp() >= 0 && size_t(oss.tellp()) > len)
1012 docstring str = oss.str();
1013 support::truncateWithEllipsis(str, len);
1018 InsetCaption const * InsetText::getCaptionInset() const
1020 ParagraphList::const_iterator pit = paragraphs().begin();
1021 for (; pit != paragraphs().end(); ++pit) {
1022 InsetList::const_iterator it = pit->insetList().begin();
1023 for (; it != pit->insetList().end(); ++it) {
1024 Inset & inset = *it->inset;
1025 if (inset.lyxCode() == CAPTION_CODE) {
1026 InsetCaption const * ins =
1027 static_cast<InsetCaption const *>(it->inset);
1036 docstring InsetText::getCaptionText(OutputParams const & runparams) const
1038 InsetCaption const * ins = getCaptionInset();
1042 odocstringstream ods;
1043 ins->getCaptionAsPlaintext(ods, runparams);
1048 docstring InsetText::getCaptionHTML(OutputParams const & runparams) const
1050 InsetCaption const * ins = getCaptionInset();
1054 odocstringstream ods;
1055 XHTMLStream xs(ods);
1056 docstring def = ins->getCaptionAsHTML(xs, runparams);
1058 // should already have been escaped
1059 xs << XHTMLStream::ESCAPE_NONE << def << '\n';
1064 InsetText::XHTMLOptions operator|(InsetText::XHTMLOptions a1, InsetText::XHTMLOptions a2)
1066 return static_cast<InsetText::XHTMLOptions>((int)a1 | (int)a2);