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