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