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