]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
Move the contents of a row into the caption when setting a row as a longtable caption.
[lyx.git] / src / insets / InsetText.cpp
1 /**
2  * \file InsetText.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetText.h"
14
15 #include "insets/InsetOptArg.h"
16
17 #include "buffer_funcs.h"
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "CompletionList.h"
22 #include "CoordCache.h"
23 #include "Cursor.h"
24 #include "CutAndPaste.h"
25 #include "DispatchResult.h"
26 #include "ErrorList.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "InsetCaption.h"
30 #include "InsetList.h"
31 #include "Intl.h"
32 #include "Language.h"
33 #include "LaTeXFeatures.h"
34 #include "Lexer.h"
35 #include "lyxfind.h"
36 #include "LyXRC.h"
37 #include "MetricsInfo.h"
38 #include "output_docbook.h"
39 #include "output_latex.h"
40 #include "output_xhtml.h"
41 #include "OutputParams.h"
42 #include "output_plaintext.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "Row.h"
47 #include "sgml.h"
48 #include "TexRow.h"
49 #include "TextClass.h"
50 #include "Text.h"
51 #include "TextMetrics.h"
52 #include "TocBackend.h"
53
54 #include "frontends/alert.h"
55 #include "frontends/Painter.h"
56
57 #include "support/debug.h"
58 #include "support/gettext.h"
59 #include "support/lstrings.h"
60
61 #include <boost/bind.hpp>
62 #include "support/lassert.h"
63
64 using namespace std;
65 using namespace lyx::support;
66
67 using boost::bind;
68 using boost::ref;
69
70 namespace lyx {
71
72 using graphics::PreviewLoader;
73
74
75 /////////////////////////////////////////////////////////////////////
76
77 InsetText::InsetText(Buffer * buf, UsePlain type)
78         : Inset(buf), drawFrame_(false), frame_color_(Color_insetframe), text_(this)
79 {
80         initParagraphs(type);
81 }
82
83
84 InsetText::InsetText(InsetText const & in)
85         : Inset(in), text_(this)
86 {
87         text_.autoBreakRows_ = in.text_.autoBreakRows_;
88         drawFrame_ = in.drawFrame_;
89         frame_color_ = in.frame_color_;
90         text_.paragraphs() = in.text_.paragraphs();
91         setParagraphOwner();
92 }
93
94
95 void InsetText::setBuffer(Buffer & buf)
96 {
97         ParagraphList::iterator end = paragraphs().end();
98         for (ParagraphList::iterator it = paragraphs().begin(); it != end; ++it)
99                 it->setBuffer(buf);
100         Inset::setBuffer(buf);
101 }
102
103
104 void InsetText::initParagraphs(UsePlain type)
105 {
106         LASSERT(paragraphs().empty(), /**/);
107         paragraphs().push_back(Paragraph());
108         Paragraph & ourpar = paragraphs().back();
109         ourpar.setInsetOwner(this);
110         DocumentClass const & dc = buffer_->params().documentClass();
111         if (type == DefaultLayout)
112                 ourpar.setDefaultLayout(dc);
113         else
114                 ourpar.setPlainLayout(dc);
115 }
116
117
118 void InsetText::setParagraphOwner()
119 {
120         for_each(paragraphs().begin(), paragraphs().end(),
121                  bind(&Paragraph::setInsetOwner, _1, this));
122 }
123
124
125 void InsetText::clear()
126 {
127         ParagraphList & pars = paragraphs();
128         LASSERT(!pars.empty(), /**/);
129
130         // This is a gross hack...
131         Layout const & old_layout = pars.begin()->layout();
132
133         pars.clear();
134         pars.push_back(Paragraph());
135         pars.begin()->setInsetOwner(this);
136         pars.begin()->setLayout(old_layout);
137 }
138
139
140 Dimension const InsetText::dimension(BufferView const & bv) const
141 {
142         TextMetrics const & tm = bv.textMetrics(&text_);
143         Dimension dim = tm.dimension();
144         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
145         dim.des += TEXT_TO_INSET_OFFSET;
146         dim.asc += TEXT_TO_INSET_OFFSET;
147         return dim;
148 }
149
150
151 void InsetText::write(ostream & os) const
152 {
153         os << "Text\n";
154         text_.write(os);
155 }
156
157
158 void InsetText::read(Lexer & lex)
159 {
160         clear();
161
162         // delete the initial paragraph
163         Paragraph oldpar = *paragraphs().begin();
164         paragraphs().clear();
165         ErrorList errorList;
166         lex.setContext("InsetText::read");
167         bool res = text_.read(lex, errorList, this);
168
169         if (!res)
170                 lex.printError("Missing \\end_inset at this point. ");
171
172         // sanity check
173         // ensure we have at least one paragraph.
174         if (paragraphs().empty())
175                 paragraphs().push_back(oldpar);
176         // Force default font, if so requested
177         // This avoids paragraphs in buffer language that would have a
178         // foreign language after a document language change, and it ensures
179         // that all new text in ERT and similar gets the "latex" language,
180         // since new text inherits the language from the last position of the
181         // existing text.  As a side effect this makes us also robust against
182         // bugs in LyX that might lead to font changes in ERT in .lyx files.
183         fixParagraphsFont();
184 }
185
186
187 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
188 {
189         TextMetrics & tm = mi.base.bv->textMetrics(&text_);
190
191         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
192
193         // Hand font through to contained lyxtext:
194         tm.font_.fontInfo() = mi.base.font;
195         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
196
197         // This can happen when a layout has a left and right margin,
198         // and the view is made very narrow. We can't do better than 
199         // to draw it partly out of view (bug 5890).
200         if (mi.base.textwidth < 1)
201                 mi.base.textwidth = 1;
202
203         if (hasFixedWidth())
204                 tm.metrics(mi, dim, mi.base.textwidth);
205         else
206                 tm.metrics(mi, dim);
207         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
208         dim.asc += TEXT_TO_INSET_OFFSET;
209         dim.des += TEXT_TO_INSET_OFFSET;
210         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
211 }
212
213
214 void InsetText::draw(PainterInfo & pi, int x, int y) const
215 {
216         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
217
218         if (drawFrame_ || pi.full_repaint) {
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                 if (pi.full_repaint)
224                         pi.pain.fillRectangle(xframe, yframe, w, h,
225                                 pi.backgroundColor(this));
226
227                 if (drawFrame_)
228                         pi.pain.rectangle(xframe, yframe, w, h, frameColor());
229         }
230         ColorCode const old_color = pi.background_color;
231         pi.background_color = pi.backgroundColor(this, false);
232
233         tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
234
235         pi.background_color = old_color;
236 }
237
238
239 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
240 {
241         pit_type const pit = front ? 0 : paragraphs().size() - 1;
242         pos_type pos = front ? 0 : paragraphs().back().size();
243
244         // if visual information is not to be ignored, move to extreme right/left
245         if (entry_from != ENTRY_DIRECTION_IGNORE) {
246                 Cursor temp_cur = cur;
247                 temp_cur.pit() = pit;
248                 temp_cur.pos() = pos;
249                 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
250                 pos = temp_cur.pos();
251         }
252
253         text_.setCursor(cur.top(), pit, pos);
254         cur.clearSelection();
255         cur.finishUndo();
256 }
257
258
259 Inset * InsetText::editXY(Cursor & cur, int x, int y)
260 {
261         return cur.bv().textMetrics(&text_).editXY(cur, x, y);
262 }
263
264
265 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
266 {
267         LYXERR(Debug::ACTION, "InsetText::doDispatch()"
268                 << " [ cmd.action = " << cmd.action << ']');
269
270         if (getLayout().isPassThru()) {
271                 // Force any new text to latex_language FIXME: This
272                 // should only be necessary in constructor, but new
273                 // paragraphs that are created by pressing enter at
274                 // the start of an existing paragraph get the buffer
275                 // language and not latex_language, so we take this
276                 // brute force approach.
277                 cur.current_font.setLanguage(latex_language);
278                 cur.real_current_font.setLanguage(latex_language);
279         }
280
281         switch (cmd.action) {
282         case LFUN_PASTE:
283         case LFUN_CLIPBOARD_PASTE:
284         case LFUN_SELECTION_PASTE:
285         case LFUN_PRIMARY_SELECTION_PASTE:
286                 text_.dispatch(cur, cmd);
287                 // If we we can only store plain text, we must reset all
288                 // attributes.
289                 // FIXME: Change only the pasted paragraphs
290                 fixParagraphsFont();
291                 break;
292
293         case LFUN_INSET_DISSOLVE: {
294                 bool const main_inset = &buffer().inset() == this;
295                 bool const target_inset = cmd.argument().empty() 
296                         || cmd.getArg(0) == insetName(lyxCode());
297                 bool const one_cell = nargs() == 1;
298
299                 if (!main_inset && target_inset && one_cell) {
300                         // Text::dissolveInset assumes that the cursor
301                         // is inside the Inset.
302                         if (&cur.inset() != this)
303                                 cur.pushBackward(*this);
304                         cur.beginUndoGroup();
305                         text_.dispatch(cur, cmd);
306                         cur.endUndoGroup();
307                 } else
308                         cur.undispatched();
309                 break;
310         }
311
312         default:
313                 text_.dispatch(cur, cmd);
314         }
315         
316         if (!cur.result().dispatched())
317                 Inset::doDispatch(cur, cmd);
318 }
319
320
321 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
322         FuncStatus & status) const
323 {
324         switch (cmd.action) {
325         case LFUN_LAYOUT:
326                 status.setEnabled(!forcePlainLayout());
327                 return true;
328
329         case LFUN_LAYOUT_PARAGRAPH:
330         case LFUN_PARAGRAPH_PARAMS:
331         case LFUN_PARAGRAPH_PARAMS_APPLY:
332         case LFUN_PARAGRAPH_UPDATE:
333                 status.setEnabled(allowParagraphCustomization());
334                 return true;
335
336         case LFUN_INSET_DISSOLVE: {
337                 bool const main_inset = &buffer().inset() == this;
338                 bool const target_inset = cmd.argument().empty() 
339                         || cmd.getArg(0) == insetName(lyxCode());
340                 bool const one_cell = nargs() == 1;
341
342                 if (target_inset)
343                         status.setEnabled(!main_inset && one_cell);
344                 return target_inset;
345         }
346
347         default:
348                 // Dispatch only to text_ if the cursor is inside
349                 // the text_. It is not for context menus (bug 5797).
350                 bool ret = false;
351                 if (cur.text() == &text_)
352                         ret = text_.getStatus(cur, cmd, status);
353                 
354                 if (!ret)
355                         ret = Inset::getStatus(cur, cmd, status);
356                 return ret;
357         }
358 }
359
360
361 void InsetText::fixParagraphsFont()
362 {
363         if (!getLayout().isPassThru())
364                 return;
365
366         Font font(inherit_font, buffer().params().language);
367         font.setLanguage(latex_language);
368         ParagraphList::iterator par = paragraphs().begin();
369         ParagraphList::iterator const end = paragraphs().end();
370         while (par != end) {
371                 par->resetFonts(font);
372                 par->params().clear();
373                 ++par;
374         }
375 }
376
377
378 void InsetText::setChange(Change const & change)
379 {
380         ParagraphList::iterator pit = paragraphs().begin();
381         ParagraphList::iterator end = paragraphs().end();
382         for (; pit != end; ++pit) {
383                 pit->setChange(change);
384         }
385 }
386
387
388 void InsetText::acceptChanges()
389 {
390         text_.acceptChanges();
391 }
392
393
394 void InsetText::rejectChanges()
395 {
396         text_.rejectChanges();
397 }
398
399
400 void InsetText::validate(LaTeXFeatures & features) const
401 {
402         features.useInsetLayout(getLayout());
403         for_each(paragraphs().begin(), paragraphs().end(),
404                  bind(&Paragraph::validate, _1, ref(features)));
405 }
406
407
408 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
409 {
410         // This implements the standard way of handling the LaTeX
411         // output of a text inset, either a command or an
412         // environment. Standard collapsable insets should not
413         // redefine this, non-standard ones may call this.
414         InsetLayout const & il = getLayout();
415         int rows = 0;
416         if (!il.latexname().empty()) {
417                 if (il.latextype() == InsetLayout::COMMAND) {
418                         // FIXME UNICODE
419                         if (runparams.moving_arg)
420                                 os << "\\protect";
421                         os << '\\' << from_utf8(il.latexname());
422                         if (!il.latexparam().empty())
423                                 os << from_utf8(il.latexparam());
424                         os << '{';
425                 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
426                         os << "%\n\\begin{" << from_utf8(il.latexname()) << "}\n";
427                         if (!il.latexparam().empty())
428                                 os << from_utf8(il.latexparam());
429                         rows += 2;
430                 }
431         }
432         OutputParams rp = runparams;
433         if (il.isPassThru())
434                 rp.verbatim = true;
435         if (il.isNeedProtect())
436                 rp.moving_arg = true;
437
438         // Output the contents of the inset
439         TexRow texrow;
440         latexParagraphs(buffer(), text_, os, texrow, rp);
441         rows += texrow.rows();
442
443         if (!il.latexname().empty()) {
444                 if (il.latextype() == InsetLayout::COMMAND) {
445                         os << "}";
446                 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
447                         os << "\n\\end{" << from_utf8(il.latexname()) << "}\n";
448                         rows += 2;
449                 }
450         }
451         return rows;
452 }
453
454
455 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
456 {
457         ParagraphList::const_iterator beg = paragraphs().begin();
458         ParagraphList::const_iterator end = paragraphs().end();
459         ParagraphList::const_iterator it = beg;
460         bool ref_printed = false;
461         int len = 0;
462         for (; it != end; ++it) {
463                 if (it != beg) {
464                         os << '\n';
465                         if (runparams.linelen > 0)
466                                 os << '\n';
467                 }
468                 odocstringstream oss;
469                 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
470                 docstring const str = oss.str();
471                 os << str;
472                 // FIXME: len is not computed fully correctly; in principle,
473                 // we have to count the characters after the last '\n'
474                 len = str.size();
475         }
476
477         return len;
478 }
479
480
481 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
482 {
483         ParagraphList::const_iterator const beg = paragraphs().begin();
484
485         if (!undefined())
486                 sgml::openTag(os, getLayout().latexname(),
487                               beg->getID(buffer(), runparams) + getLayout().latexparam());
488
489         docbookParagraphs(text_, buffer(), os, runparams);
490
491         if (!undefined())
492                 sgml::closeTag(os, getLayout().latexname());
493
494         return 0;
495 }
496
497
498 docstring InsetText::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
499 {
500         return insetAsXHTML(xs, runparams, WriteEverything);
501 }
502
503
504 // FIXME XHTML
505 // There are cases where we may need to close open fonts and such
506 // and then re-open them when we are done. This would be the case, e.g.,
507 // if we were otherwise about to write:
508 //              <em>word <div class='foot'>footnote text.</div> emph</em>
509 // The problem isn't so much that the footnote text will get emphasized:
510 // we can handle that with CSS. The problem is that this is invalid XHTML.
511 // One solution would be to make the footnote <span>, but the problem is
512 // completely general, and so we'd have to make absolutely everything into
513 // span. What I think will work is to check if we're about to write "div" and,
514 // if so, try to close fonts, etc. 
515 // There are probably limits to how well we can do here, though, and we will
516 // have to rely upon users not putting footnotes inside noun-type insets.
517 docstring InsetText::insetAsXHTML(XHTMLStream & xs, OutputParams const & runparams,
518                                   XHTMLOptions opts) const
519 {
520         if (undefined()) {
521                 xhtmlParagraphs(text_, buffer(), xs, runparams);
522                 return docstring();
523         }
524
525         InsetLayout const & il = getLayout();
526         if (opts & WriteOuterTag)
527                 xs << StartTag(il.htmltag(), il.htmlattr());
528         if ((opts & WriteLabel) && !il.counter().empty()) {
529                 BufferParams const & bp = buffer().masterBuffer()->params();
530                 Counters & cntrs = bp.documentClass().counters();
531                 cntrs.step(il.counter());
532                 // FIXME: translate to paragraph language
533                 if (!il.htmllabel().empty()) {
534                         docstring const lbl = 
535                                 cntrs.counterLabel(from_utf8(il.htmllabel()), bp.language->code());
536                         // FIXME is this check necessary?
537                         if (!lbl.empty()) {
538                                 xs << StartTag(il.htmllabeltag(), il.htmllabelattr());
539                                 xs << lbl;
540                                 xs << EndTag(il.htmllabeltag());
541                         }
542                 }
543         }
544
545         if (opts & WriteInnerTag)
546                 xs << StartTag(il.htmlinnertag(), il.htmlinnerattr());
547         if (il.isMultiPar())
548                 xhtmlParagraphs(text_, buffer(), xs, runparams);
549         else {
550                 OutputParams ours = runparams;
551                 ours.html_make_pars = false;
552                 xhtmlParagraphs(text_, buffer(), xs, ours);
553         }
554         if (opts & WriteInnerTag)
555                 xs << EndTag(il.htmlinnertag());
556         if (opts & WriteOuterTag)
557                 xs << EndTag(il.htmltag());
558         return docstring();
559 }
560
561
562 void InsetText::cursorPos(BufferView const & bv,
563                 CursorSlice const & sl, bool boundary, int & x, int & y) const
564 {
565         x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
566         y = bv.textMetrics(&text_).cursorY(sl, boundary);
567 }
568
569
570 bool InsetText::showInsetDialog(BufferView *) const
571 {
572         return false;
573 }
574
575
576 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
577 {
578         clear();
579         Paragraph & first = paragraphs().front();
580         for (unsigned int i = 0; i < data.length(); ++i)
581                 first.insertChar(i, data[i], font, trackChanges);
582 }
583
584
585 void InsetText::setAutoBreakRows(bool flag)
586 {
587         if (flag == text_.autoBreakRows_)
588                 return;
589
590         text_.autoBreakRows_ = flag;
591         if (flag)
592                 return;
593
594         // remove previously existing newlines
595         ParagraphList::iterator it = paragraphs().begin();
596         ParagraphList::iterator end = paragraphs().end();
597         for (; it != end; ++it)
598                 for (int i = 0; i < it->size(); ++i)
599                         if (it->isNewline(i))
600                                 // do not track the change, because the user
601                                 // is not allowed to revert/reject it
602                                 it->eraseChar(i, false);
603 }
604
605
606 void InsetText::setDrawFrame(bool flag)
607 {
608         drawFrame_ = flag;
609 }
610
611
612 ColorCode InsetText::frameColor() const
613 {
614         return frame_color_;
615 }
616
617
618 void InsetText::setFrameColor(ColorCode col)
619 {
620         frame_color_ = col;
621 }
622
623
624 void InsetText::appendParagraphs(ParagraphList & plist)
625 {
626         // There is little we can do here to keep track of changes.
627         // As of 2006/10/20, appendParagraphs is used exclusively by
628         // LyXTabular::setMultiColumn. In this context, the paragraph break
629         // is lost irreversibly and the appended text doesn't really change
630
631         ParagraphList & pl = paragraphs();
632
633         ParagraphList::iterator pit = plist.begin();
634         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
635         ++pit;
636         mergeParagraph(buffer().params(), pl,
637                        distance(pl.begin(), ins) - 1);
638
639         for_each(pit, plist.end(),
640                  bind(&ParagraphList::push_back, ref(pl), _1));
641 }
642
643
644 void InsetText::addPreview(PreviewLoader & loader) const
645 {
646         ParagraphList::const_iterator pit = paragraphs().begin();
647         ParagraphList::const_iterator pend = paragraphs().end();
648
649         for (; pit != pend; ++pit) {
650                 InsetList::const_iterator it  = pit->insetList().begin();
651                 InsetList::const_iterator end = pit->insetList().end();
652                 for (; it != end; ++it)
653                         it->inset->addPreview(loader);
654         }
655 }
656
657
658 ParagraphList const & InsetText::paragraphs() const
659 {
660         return text_.paragraphs();
661 }
662
663
664 ParagraphList & InsetText::paragraphs()
665 {
666         return text_.paragraphs();
667 }
668
669
670 void InsetText::updateLabels(ParIterator const & it)
671 {
672         ParIterator it2 = it;
673         it2.forwardPos();
674         LASSERT(&it2.inset() == this && it2.pit() == 0, return);
675         if (producesOutput())
676                 buffer().updateLabels(it2);
677         else {
678                 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
679                 Counters const savecnt = tclass.counters();
680                 buffer().updateLabels(it2);
681                 tclass.counters() = savecnt;
682         }
683 }
684
685
686 void InsetText::tocString(odocstream & os) const
687 {
688         if (!getLayout().isInToc())
689                 return;
690         os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
691 }
692
693
694
695 void InsetText::addToToc(DocIterator const & cdit)
696 {
697         DocIterator dit = cdit;
698         dit.push_back(CursorSlice(*this));
699         Toc & toc = buffer().tocBackend().toc("tableofcontents");
700
701         BufferParams const & bufparams = buffer_->params();
702         const int min_toclevel = bufparams.documentClass().min_toclevel();
703
704         // For each paragraph, traverse its insets and let them add
705         // their toc items
706         ParagraphList & pars = paragraphs();
707         pit_type pend = paragraphs().size();
708         for (pit_type pit = 0; pit != pend; ++pit) {
709                 Paragraph const & par = pars[pit];
710                 dit.pit() = pit;
711                 // the string that goes to the toc (could be the optarg)
712                 docstring tocstring;
713                 InsetList::const_iterator it  = par.insetList().begin();
714                 InsetList::const_iterator end = par.insetList().end();
715                 for (; it != end; ++it) {
716                         Inset & inset = *it->inset;
717                         dit.pos() = it->pos;
718                         //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
719                         inset.addToToc(dit);
720                         switch (inset.lyxCode()) {
721                         case OPTARG_CODE: {
722                                 if (!tocstring.empty())
723                                         break;
724                                 dit.pos() = 0;
725                                 Paragraph const & insetpar =
726                                         *static_cast<InsetOptArg&>(inset).paragraphs().begin();
727                                 if (!par.labelString().empty())
728                                         tocstring = par.labelString() + ' ';
729                                 tocstring += insetpar.asString(AS_STR_INSETS);
730                                 break;
731                         }
732                         default:
733                                 break;
734                         }
735                 }
736                 // now the toc entry for the paragraph
737                 int const toclevel = par.layout().toclevel;
738                 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
739                         dit.pos() = 0;
740                         // insert this into the table of contents
741                         if (tocstring.empty())
742                                 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
743                         toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
744                 }
745                 
746                 // And now the list of changes.
747                 par.addChangesToToc(dit, buffer());
748         }
749 }
750
751
752 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
753 {
754         if (buffer().isClean())
755                 return Inset::notifyCursorLeaves(old, cur);
756         
757         // find text inset in old cursor
758         Cursor insetCur = old;
759         int scriptSlice = insetCur.find(this);
760         LASSERT(scriptSlice != -1, /**/);
761         insetCur.cutOff(scriptSlice);
762         LASSERT(&insetCur.inset() == this, /**/);
763         
764         // update the old paragraph's words
765         insetCur.paragraph().updateWords();
766         
767         return Inset::notifyCursorLeaves(old, cur);
768 }
769
770
771 bool InsetText::completionSupported(Cursor const & cur) const
772 {
773         //LASSERT(&cur.bv().cursor().inset() != this, return false);
774         return text_.completionSupported(cur);
775 }
776
777
778 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
779 {
780         return completionSupported(cur);
781 }
782
783
784 bool InsetText::automaticInlineCompletion() const
785 {
786         return lyxrc.completion_inline_text;
787 }
788
789
790 bool InsetText::automaticPopupCompletion() const
791 {
792         return lyxrc.completion_popup_text;
793 }
794
795
796 bool InsetText::showCompletionCursor() const
797 {
798         return lyxrc.completion_cursor_text;
799 }
800
801
802 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
803 {
804         return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
805 }
806
807
808 docstring InsetText::completionPrefix(Cursor const & cur) const
809 {
810         if (!completionSupported(cur))
811                 return docstring();
812         return text_.completionPrefix(cur);
813 }
814
815
816 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
817         bool finished)
818 {
819         if (!completionSupported(cur))
820                 return false;
821
822         return text_.insertCompletion(cur, s, finished);
823 }
824
825
826 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y, 
827         Dimension & dim) const
828 {
829         TextMetrics const & tm = cur.bv().textMetrics(&text_);
830         tm.completionPosAndDim(cur, x, y, dim);
831 }
832
833
834 docstring InsetText::contextMenu(BufferView const &, int, int) const
835 {
836         return from_ascii("context-edit");
837 }
838
839
840 InsetCaption const * InsetText::getCaptionInset() const
841 {
842         ParagraphList::const_iterator pit = paragraphs().begin();
843         for (; pit != paragraphs().end(); ++pit) {
844                 InsetList::const_iterator it = pit->insetList().begin();
845                 for (; it != pit->insetList().end(); ++it) {
846                         Inset & inset = *it->inset;
847                         if (inset.lyxCode() == CAPTION_CODE) {
848                                 InsetCaption const * ins =
849                                         static_cast<InsetCaption const *>(it->inset);
850                                 return ins;
851                         }
852                 }
853         }
854         return 0;
855 }
856
857
858 docstring InsetText::getCaptionText(OutputParams const & runparams) const
859 {
860         InsetCaption const * ins = getCaptionInset();
861         if (ins == 0)
862                 return docstring();
863
864         odocstringstream ods;
865         ins->getCaptionAsPlaintext(ods, runparams);
866         return ods.str();
867 }
868
869
870 docstring InsetText::getCaptionHTML(OutputParams const & runparams) const
871 {
872         InsetCaption const * ins = getCaptionInset();
873         if (ins == 0)
874                 return docstring();
875
876         odocstringstream ods;
877         docstring def = ins->getCaptionAsHTML(ods, runparams);
878         if (!def.empty())
879                 ods << def << '\n';
880         return ods.str();
881 }
882
883
884 } // namespace lyx