]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
114aa5233f3b74f44fe7f87aafde280d76301ab1
[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         if (getLayout().isPassThru()) {
273                 // Force any new text to latex_language FIXME: This
274                 // should only be necessary in constructor, but new
275                 // paragraphs that are created by pressing enter at
276                 // the start of an existing paragraph get the buffer
277                 // language and not latex_language, so we take this
278                 // brute force approach.
279                 cur.current_font.setLanguage(latex_language);
280                 cur.real_current_font.setLanguage(latex_language);
281         }
282
283         switch (cmd.action) {
284         case LFUN_PASTE:
285         case LFUN_CLIPBOARD_PASTE:
286         case LFUN_SELECTION_PASTE:
287         case LFUN_PRIMARY_SELECTION_PASTE:
288                 text_.dispatch(cur, cmd);
289                 // If we we can only store plain text, we must reset all
290                 // attributes.
291                 // FIXME: Change only the pasted paragraphs
292                 fixParagraphsFont();
293                 break;
294         default:
295                 text_.dispatch(cur, cmd);
296         }
297         
298         if (!cur.result().dispatched())
299                 Inset::doDispatch(cur, cmd);
300 }
301
302
303 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
304         FuncStatus & status) const
305 {
306         switch (cmd.action) {
307         case LFUN_LAYOUT:
308                 status.setEnabled(!forcePlainLayout());
309                 return true;
310
311         case LFUN_LAYOUT_PARAGRAPH:
312         case LFUN_PARAGRAPH_PARAMS:
313         case LFUN_PARAGRAPH_PARAMS_APPLY:
314         case LFUN_PARAGRAPH_UPDATE:
315                 status.setEnabled(allowParagraphCustomization());
316                 return true;
317         default:
318                 // Dispatch only to text_ if the cursor is inside
319                 // the text_. It is not for context menus (bug 5797).
320                 bool ret = false;
321                 if (cur.text() == &text_)
322                         ret = text_.getStatus(cur, cmd, status);
323                 
324                 if (!ret)
325                         ret = Inset::getStatus(cur, cmd, status);
326                 return ret;
327         }
328 }
329
330
331 void InsetText::fixParagraphsFont()
332 {
333         Font font(inherit_font, buffer().params().language);
334         if (getLayout().isPassThru()) {
335                 font.setLanguage(latex_language);
336                 ParagraphList::iterator par = paragraphs().begin();
337                 ParagraphList::iterator const end = paragraphs().end();
338                 while (par != end) {
339                         par->resetFonts(font);
340                         par->params().clear();
341                         ++par;
342                 }
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(buffer().params());
360 }
361
362
363 void InsetText::rejectChanges()
364 {
365         text_.rejectChanges(buffer().params());
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