]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
8247d57d894ba8d893764dfdd75b2e35a7352a36
[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), text_(this)
80 {
81         setBuffer(const_cast<Buffer &>(buf));
82         initParagraphs(type);
83 }
84
85
86 InsetText::InsetText(InsetText const & in)
87         : Inset(in), text_(this)
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         if (!getLayout().isPassThru())
334                 return;
335
336         Font font(inherit_font, buffer().params().language);
337         font.setLanguage(latex_language);
338         ParagraphList::iterator par = paragraphs().begin();
339         ParagraphList::iterator const end = paragraphs().end();
340         while (par != end) {
341                 par->resetFonts(font);
342                 par->params().clear();
343                 ++par;
344         }
345 }
346
347
348 void InsetText::setChange(Change const & change)
349 {
350         ParagraphList::iterator pit = paragraphs().begin();
351         ParagraphList::iterator end = paragraphs().end();
352         for (; pit != end; ++pit) {
353                 pit->setChange(change);
354         }
355 }
356
357
358 void InsetText::acceptChanges()
359 {
360         text_.acceptChanges(buffer().params());
361 }
362
363
364 void InsetText::rejectChanges()
365 {
366         text_.rejectChanges(buffer().params());
367 }
368
369
370 void InsetText::validate(LaTeXFeatures & features) const
371 {
372         features.useInsetLayout(getLayout());
373         for_each(paragraphs().begin(), paragraphs().end(),
374                  bind(&Paragraph::validate, _1, ref(features)));
375 }
376
377
378 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
379 {
380         // This implements the standard way of handling the LaTeX
381         // output of a text inset, either a command or an
382         // environment. Standard collapsable insets should not
383         // redefine this, non-standard ones may call this.
384         InsetLayout const & il = getLayout();
385         int rows = 0;
386         if (!il.latexname().empty()) {
387                 if (il.latextype() == InsetLayout::COMMAND) {
388                         // FIXME UNICODE
389                         if (runparams.moving_arg)
390                                 os << "\\protect";
391                         os << '\\' << from_utf8(il.latexname());
392                         if (!il.latexparam().empty())
393                                 os << from_utf8(il.latexparam());
394                         os << '{';
395                 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
396                         os << "%\n\\begin{" << from_utf8(il.latexname()) << "}\n";
397                         if (!il.latexparam().empty())
398                                 os << from_utf8(il.latexparam());
399                         rows += 2;
400                 }
401         }
402         OutputParams rp = runparams;
403         if (il.isPassThru())
404                 rp.verbatim = true;
405         if (il.isNeedProtect())
406                 rp.moving_arg = true;
407
408         // Output the contents of the inset
409         TexRow texrow;
410         latexParagraphs(buffer(), text_, os, texrow, rp);
411         rows += texrow.rows();
412
413         if (!il.latexname().empty()) {
414                 if (il.latextype() == InsetLayout::COMMAND) {
415                         os << "}";
416                 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
417                         os << "\n\\end{" << from_utf8(il.latexname()) << "}\n";
418                         rows += 2;
419                 }
420         }
421         return rows;
422 }
423
424
425 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
426 {
427         ParagraphList::const_iterator beg = paragraphs().begin();
428         ParagraphList::const_iterator end = paragraphs().end();
429         ParagraphList::const_iterator it = beg;
430         bool ref_printed = false;
431         int len = 0;
432         for (; it != end; ++it) {
433                 if (it != beg) {
434                         os << '\n';
435                         if (runparams.linelen > 0)
436                                 os << '\n';
437                 }
438                 odocstringstream oss;
439                 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
440                 docstring const str = oss.str();
441                 os << str;
442                 // FIXME: len is not computed fully correctly; in principle,
443                 // we have to count the characters after the last '\n'
444                 len = str.size();
445         }
446
447         return len;
448 }
449
450
451 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
452 {
453         ParagraphList::const_iterator const beg = paragraphs().begin();
454
455         if (!undefined())
456                 sgml::openTag(os, getLayout().latexname(),
457                               beg->getID(buffer(), runparams) + getLayout().latexparam());
458
459         docbookParagraphs(paragraphs(), buffer(), os, runparams);
460
461         if (!undefined())
462                 sgml::closeTag(os, getLayout().latexname());
463
464         return 0;
465 }
466
467
468 docstring InsetText::xhtml(odocstream & os, OutputParams const & runparams) const
469 {
470         InsetLayout const & il = getLayout();
471         if (undefined()) {
472                 xhtmlParagraphs(paragraphs(), buffer(), os, runparams);
473                 return docstring();
474         }
475
476         bool const opened = html::openTag(os, il.htmltag(), il.htmlattr());
477         if (!il.counter().empty()) {
478                 BufferParams const & bp = buffer().masterBuffer()->params();
479                 Counters & cntrs = bp.documentClass().counters();
480                 cntrs.step(il.counter());
481                 // FIXME: translate to paragraph language
482                 if (!il.htmllabel().empty())
483                         os << cntrs.counterLabel(from_utf8(il.htmllabel()), bp.language->code());
484         }
485         bool innertag_opened = false;
486         if (!il.htmlinnertag().empty())
487                 innertag_opened = html::openTag(os, il.htmlinnertag(), il.htmlinnerattr());
488
489         xhtmlParagraphs(paragraphs(), buffer(), os, runparams);
490
491         if (innertag_opened)
492                 html::closeTag(os, il.htmlinnertag());
493         if (opened)
494                 html::closeTag(os, il.htmltag());
495         return docstring();
496 }
497
498
499 void InsetText::cursorPos(BufferView const & bv,
500                 CursorSlice const & sl, bool boundary, int & x, int & y) const
501 {
502         x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
503         y = bv.textMetrics(&text_).cursorY(sl, boundary);
504 }
505
506
507 bool InsetText::showInsetDialog(BufferView *) const
508 {
509         return false;
510 }
511
512
513 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
514 {
515         clear();
516         Paragraph & first = paragraphs().front();
517         for (unsigned int i = 0; i < data.length(); ++i)
518                 first.insertChar(i, data[i], font, trackChanges);
519 }
520
521
522 void InsetText::setAutoBreakRows(bool flag)
523 {
524         if (flag == text_.autoBreakRows_)
525                 return;
526
527         text_.autoBreakRows_ = flag;
528         if (flag)
529                 return;
530
531         // remove previously existing newlines
532         ParagraphList::iterator it = paragraphs().begin();
533         ParagraphList::iterator end = paragraphs().end();
534         for (; it != end; ++it)
535                 for (int i = 0; i < it->size(); ++i)
536                         if (it->isNewline(i))
537                                 // do not track the change, because the user
538                                 // is not allowed to revert/reject it
539                                 it->eraseChar(i, false);
540 }
541
542
543 void InsetText::setDrawFrame(bool flag)
544 {
545         drawFrame_ = flag;
546 }
547
548
549 ColorCode InsetText::frameColor() const
550 {
551         return frame_color_;
552 }
553
554
555 void InsetText::setFrameColor(ColorCode col)
556 {
557         frame_color_ = col;
558 }
559
560
561 void InsetText::appendParagraphs(ParagraphList & plist)
562 {
563         // There is little we can do here to keep track of changes.
564         // As of 2006/10/20, appendParagraphs is used exclusively by
565         // LyXTabular::setMultiColumn. In this context, the paragraph break
566         // is lost irreversibly and the appended text doesn't really change
567
568         ParagraphList & pl = paragraphs();
569
570         ParagraphList::iterator pit = plist.begin();
571         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
572         ++pit;
573         mergeParagraph(buffer().params(), pl,
574                        distance(pl.begin(), ins) - 1);
575
576         for_each(pit, plist.end(),
577                  bind(&ParagraphList::push_back, ref(pl), _1));
578 }
579
580
581 void InsetText::addPreview(PreviewLoader & loader) const
582 {
583         ParagraphList::const_iterator pit = paragraphs().begin();
584         ParagraphList::const_iterator pend = paragraphs().end();
585
586         for (; pit != pend; ++pit) {
587                 InsetList::const_iterator it  = pit->insetList().begin();
588                 InsetList::const_iterator end = pit->insetList().end();
589                 for (; it != end; ++it)
590                         it->inset->addPreview(loader);
591         }
592 }
593
594
595 ParagraphList const & InsetText::paragraphs() const
596 {
597         return text_.paragraphs();
598 }
599
600
601 ParagraphList & InsetText::paragraphs()
602 {
603         return text_.paragraphs();
604 }
605
606
607 void InsetText::updateLabels(ParIterator const & it)
608 {
609         ParIterator it2 = it;
610         it2.forwardPos();
611         LASSERT(&it2.inset() == this && it2.pit() == 0, return);
612         if (producesOutput())
613                 buffer().updateLabels(it2);
614         else {
615                 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
616                 Counters const savecnt = tclass.counters();
617                 buffer().updateLabels(it2);
618                 tclass.counters() = savecnt;
619         }
620 }
621
622
623 void InsetText::tocString(odocstream & os) const
624 {
625         if (!getLayout().isInToc())
626                 return;
627         os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
628 }
629
630
631
632 void InsetText::addToToc(DocIterator const & cdit)
633 {
634         DocIterator dit = cdit;
635         dit.push_back(CursorSlice(*this));
636         Toc & toc = buffer().tocBackend().toc("tableofcontents");
637
638         BufferParams const & bufparams = buffer_->params();
639         const int min_toclevel = bufparams.documentClass().min_toclevel();
640
641         // For each paragraph, traverse its insets and let them add
642         // their toc items
643         ParagraphList & pars = paragraphs();
644         pit_type pend = paragraphs().size();
645         for (pit_type pit = 0; pit != pend; ++pit) {
646                 Paragraph const & par = pars[pit];
647                 dit.pit() = pit;
648                 // the string that goes to the toc (could be the optarg)
649                 docstring tocstring;
650                 InsetList::const_iterator it  = par.insetList().begin();
651                 InsetList::const_iterator end = par.insetList().end();
652                 for (; it != end; ++it) {
653                         Inset & inset = *it->inset;
654                         dit.pos() = it->pos;
655                         //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
656                         inset.addToToc(dit);
657                         switch (inset.lyxCode()) {
658                         case OPTARG_CODE: {
659                                 if (!tocstring.empty())
660                                         break;
661                                 dit.pos() = 0;
662                                 Paragraph const & insetpar =
663                                         *static_cast<InsetOptArg&>(inset).paragraphs().begin();
664                                 if (!par.labelString().empty())
665                                         tocstring = par.labelString() + ' ';
666                                 tocstring += insetpar.asString(AS_STR_INSETS);
667                                 break;
668                         }
669                         default:
670                                 break;
671                         }
672                 }
673                 // now the toc entry for the paragraph
674                 int const toclevel = par.layout().toclevel;
675                 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
676                         dit.pos() = 0;
677                         // insert this into the table of contents
678                         if (tocstring.empty())
679                                 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
680                         toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
681                 }
682                 
683                 // And now the list of changes.
684                 par.addChangesToToc(dit, buffer());
685         }
686 }
687
688
689 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
690 {
691         if (buffer().isClean())
692                 return Inset::notifyCursorLeaves(old, cur);
693         
694         // find text inset in old cursor
695         Cursor insetCur = old;
696         int scriptSlice = insetCur.find(this);
697         LASSERT(scriptSlice != -1, /**/);
698         insetCur.cutOff(scriptSlice);
699         LASSERT(&insetCur.inset() == this, /**/);
700         
701         // update the old paragraph's words
702         insetCur.paragraph().updateWords();
703         
704         return Inset::notifyCursorLeaves(old, cur);
705 }
706
707
708 bool InsetText::completionSupported(Cursor const & cur) const
709 {
710         //LASSERT(&cur.bv().cursor().inset() != this, return false);
711         return text_.completionSupported(cur);
712 }
713
714
715 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
716 {
717         return completionSupported(cur);
718 }
719
720
721 bool InsetText::automaticInlineCompletion() const
722 {
723         return lyxrc.completion_inline_text;
724 }
725
726
727 bool InsetText::automaticPopupCompletion() const
728 {
729         return lyxrc.completion_popup_text;
730 }
731
732
733 bool InsetText::showCompletionCursor() const
734 {
735         return lyxrc.completion_cursor_text;
736 }
737
738
739 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
740 {
741         return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
742 }
743
744
745 docstring InsetText::completionPrefix(Cursor const & cur) const
746 {
747         if (!completionSupported(cur))
748                 return docstring();
749         return text_.completionPrefix(cur);
750 }
751
752
753 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
754         bool finished)
755 {
756         if (!completionSupported(cur))
757                 return false;
758
759         return text_.insertCompletion(cur, s, finished);
760 }
761
762
763 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y, 
764         Dimension & dim) const
765 {
766         TextMetrics const & tm = cur.bv().textMetrics(&text_);
767         tm.completionPosAndDim(cur, x, y, dim);
768 }
769
770
771 docstring InsetText::contextMenu(BufferView const &, int, int) const
772 {
773         return from_ascii("context-edit");
774 }
775
776
777 InsetCaption const * InsetText::getCaptionInset() const
778 {
779         ParagraphList::const_iterator pit = paragraphs().begin();
780         for (; pit != paragraphs().end(); ++pit) {
781                 InsetList::const_iterator it = pit->insetList().begin();
782                 for (; it != pit->insetList().end(); ++it) {
783                         Inset & inset = *it->inset;
784                         if (inset.lyxCode() == CAPTION_CODE) {
785                                 InsetCaption const * ins =
786                                         static_cast<InsetCaption const *>(it->inset);
787                                 return ins;
788                         }
789                 }
790         }
791         return 0;
792 }
793
794
795 docstring InsetText::getCaptionText(OutputParams const & runparams) const
796 {
797         InsetCaption const * ins = getCaptionInset();
798         if (ins == 0)
799                 return docstring();
800
801         odocstringstream ods;
802         ins->getCaptionAsPlaintext(ods, runparams);
803         return ods.str();
804 }
805
806
807 docstring InsetText::getCaptionHTML(OutputParams const & runparams) const
808 {
809         InsetCaption const * ins = getCaptionInset();
810         if (ins == 0)
811                 return docstring();
812
813         odocstringstream ods;
814         docstring def = ins->getCaptionAsHTML(ods, runparams);
815         if (!def.empty())
816                 ods << def << '\n';
817         return ods.str();
818 }
819
820
821 } // namespace lyx