]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
6b2f81d842a5a86a1eaf00ae17c3fadabca40f06
[lyx.git] / src / insets / InsetText.cpp
1 /**
2  * \file InsetText.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetText.h"
14
15 #include "insets/InsetOptArg.h"
16
17 #include "buffer_funcs.h"
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "CompletionList.h"
22 #include "CoordCache.h"
23 #include "Cursor.h"
24 #include "CutAndPaste.h"
25 #include "DispatchResult.h"
26 #include "ErrorList.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "InsetCaption.h"
30 #include "InsetList.h"
31 #include "Intl.h"
32 #include "Language.h"
33 #include "LaTeXFeatures.h"
34 #include "Lexer.h"
35 #include "lyxfind.h"
36 #include "LyXRC.h"
37 #include "MetricsInfo.h"
38 #include "output_docbook.h"
39 #include "output_latex.h"
40 #include "output_xhtml.h"
41 #include "OutputParams.h"
42 #include "output_plaintext.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "Row.h"
47 #include "sgml.h"
48 #include "TexRow.h"
49 #include "TextClass.h"
50 #include "Text.h"
51 #include "TextMetrics.h"
52 #include "TocBackend.h"
53
54 #include "frontends/alert.h"
55 #include "frontends/Painter.h"
56
57 #include "support/debug.h"
58 #include "support/gettext.h"
59 #include "support/lstrings.h"
60
61 #include <boost/bind.hpp>
62 #include "support/lassert.h"
63
64 using namespace std;
65 using namespace lyx::support;
66
67 using boost::bind;
68 using boost::ref;
69
70 namespace lyx {
71
72 using graphics::PreviewLoader;
73
74
75 /////////////////////////////////////////////////////////////////////
76
77 InsetText::InsetText(Buffer 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
294         case LFUN_INSET_DISSOLVE: {
295                 bool const main_inset = &buffer().inset() == this;
296                 bool const target_inset = cmd.argument().empty() 
297                         || cmd.getArg(0) == insetName(lyxCode());
298                 bool const one_cell = cur.inset().nargs() == 1;
299
300                 if (!main_inset && target_inset && one_cell) {
301                         // Text::dissolveInset assumes that the cursor
302                         // is inside the Inset.
303                         if (&cur.inset() != this)
304                                 cur.pushBackward(*this);
305                         cur.beginUndoGroup();
306                         text_.dispatch(cur, cmd);
307                         cur.endUndoGroup();
308                 } else
309                         cur.undispatched();
310                 break;
311         }
312
313         default:
314                 text_.dispatch(cur, cmd);
315         }
316         
317         if (!cur.result().dispatched())
318                 Inset::doDispatch(cur, cmd);
319 }
320
321
322 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
323         FuncStatus & status) const
324 {
325         switch (cmd.action) {
326         case LFUN_LAYOUT:
327                 status.setEnabled(!forcePlainLayout());
328                 return true;
329
330         case LFUN_LAYOUT_PARAGRAPH:
331         case LFUN_PARAGRAPH_PARAMS:
332         case LFUN_PARAGRAPH_PARAMS_APPLY:
333         case LFUN_PARAGRAPH_UPDATE:
334                 status.setEnabled(allowParagraphCustomization());
335                 return true;
336
337         case LFUN_INSET_DISSOLVE: {
338                 bool const main_inset = &buffer().inset() == this;
339                 bool const target_inset = cmd.argument().empty() 
340                         || cmd.getArg(0) == insetName(lyxCode());
341                 bool const one_cell = cur.inset().nargs() == 1;
342
343                 return !main_inset && target_inset && one_cell;
344         }
345
346         default:
347                 // Dispatch only to text_ if the cursor is inside
348                 // the text_. It is not for context menus (bug 5797).
349                 bool ret = false;
350                 if (cur.text() == &text_)
351                         ret = text_.getStatus(cur, cmd, status);
352                 
353                 if (!ret)
354                         ret = Inset::getStatus(cur, cmd, status);
355                 return ret;
356         }
357 }
358
359
360 void InsetText::fixParagraphsFont()
361 {
362         if (!getLayout().isPassThru())
363                 return;
364
365         Font font(inherit_font, buffer().params().language);
366         font.setLanguage(latex_language);
367         ParagraphList::iterator par = paragraphs().begin();
368         ParagraphList::iterator const end = paragraphs().end();
369         while (par != end) {
370                 par->resetFonts(font);
371                 par->params().clear();
372                 ++par;
373         }
374 }
375
376
377 void InsetText::setChange(Change const & change)
378 {
379         ParagraphList::iterator pit = paragraphs().begin();
380         ParagraphList::iterator end = paragraphs().end();
381         for (; pit != end; ++pit) {
382                 pit->setChange(change);
383         }
384 }
385
386
387 void InsetText::acceptChanges()
388 {
389         text_.acceptChanges();
390 }
391
392
393 void InsetText::rejectChanges()
394 {
395         text_.rejectChanges();
396 }
397
398
399 void InsetText::validate(LaTeXFeatures & features) const
400 {
401         features.useInsetLayout(getLayout());
402         for_each(paragraphs().begin(), paragraphs().end(),
403                  bind(&Paragraph::validate, _1, ref(features)));
404 }
405
406
407 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
408 {
409         // This implements the standard way of handling the LaTeX
410         // output of a text inset, either a command or an
411         // environment. Standard collapsable insets should not
412         // redefine this, non-standard ones may call this.
413         InsetLayout const & il = getLayout();
414         int rows = 0;
415         if (!il.latexname().empty()) {
416                 if (il.latextype() == InsetLayout::COMMAND) {
417                         // FIXME UNICODE
418                         if (runparams.moving_arg)
419                                 os << "\\protect";
420                         os << '\\' << from_utf8(il.latexname());
421                         if (!il.latexparam().empty())
422                                 os << from_utf8(il.latexparam());
423                         os << '{';
424                 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
425                         os << "%\n\\begin{" << from_utf8(il.latexname()) << "}\n";
426                         if (!il.latexparam().empty())
427                                 os << from_utf8(il.latexparam());
428                         rows += 2;
429                 }
430         }
431         OutputParams rp = runparams;
432         if (il.isPassThru())
433                 rp.verbatim = true;
434         if (il.isNeedProtect())
435                 rp.moving_arg = true;
436
437         // Output the contents of the inset
438         TexRow texrow;
439         latexParagraphs(buffer(), text_, os, texrow, rp);
440         rows += texrow.rows();
441
442         if (!il.latexname().empty()) {
443                 if (il.latextype() == InsetLayout::COMMAND) {
444                         os << "}";
445                 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
446                         os << "\n\\end{" << from_utf8(il.latexname()) << "}\n";
447                         rows += 2;
448                 }
449         }
450         return rows;
451 }
452
453
454 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
455 {
456         ParagraphList::const_iterator beg = paragraphs().begin();
457         ParagraphList::const_iterator end = paragraphs().end();
458         ParagraphList::const_iterator it = beg;
459         bool ref_printed = false;
460         int len = 0;
461         for (; it != end; ++it) {
462                 if (it != beg) {
463                         os << '\n';
464                         if (runparams.linelen > 0)
465                                 os << '\n';
466                 }
467                 odocstringstream oss;
468                 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
469                 docstring const str = oss.str();
470                 os << str;
471                 // FIXME: len is not computed fully correctly; in principle,
472                 // we have to count the characters after the last '\n'
473                 len = str.size();
474         }
475
476         return len;
477 }
478
479
480 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
481 {
482         ParagraphList::const_iterator const beg = paragraphs().begin();
483
484         if (!undefined())
485                 sgml::openTag(os, getLayout().latexname(),
486                               beg->getID(buffer(), runparams) + getLayout().latexparam());
487
488         docbookParagraphs(text_, buffer(), os, runparams);
489
490         if (!undefined())
491                 sgml::closeTag(os, getLayout().latexname());
492
493         return 0;
494 }
495
496
497 docstring InsetText::xhtml(odocstream & os, OutputParams const & runparams) const
498 {
499         if (undefined()) {
500                 xhtmlParagraphs(text_, buffer(), os, runparams);
501                 return docstring();
502         }
503
504         InsetLayout const & il = getLayout();
505         bool const opened = html::openTag(os, il.htmltag(), il.htmlattr());
506         if (!il.counter().empty()) {
507                 BufferParams const & bp = buffer().masterBuffer()->params();
508                 Counters & cntrs = bp.documentClass().counters();
509                 cntrs.step(il.counter());
510                 // FIXME: translate to paragraph language
511                 if (!il.htmllabel().empty()) {
512                         docstring const lbl = 
513                                 cntrs.counterLabel(from_utf8(il.htmllabel()), bp.language->code());
514                         // FIXME is this check necessary?
515                         if (!lbl.empty()) {
516                                 bool const lopen = html::openTag(os, il.htmllabeltag(), il.htmllabelattr());
517                                 os << lbl;
518                                 if (lopen)
519                                         html::closeTag(os, il.htmllabeltag());
520                         }
521                 }
522         }
523
524         bool innertag_opened = false;
525         if (!il.htmlinnertag().empty())
526                 innertag_opened = html::openTag(os, il.htmlinnertag(), il.htmlinnerattr());
527
528         xhtmlParagraphs(text_, buffer(), os, runparams);
529
530         if (innertag_opened)
531                 html::closeTag(os, il.htmlinnertag());
532         if (opened)
533                 html::closeTag(os, il.htmltag());
534         return docstring();
535 }
536
537
538 void InsetText::cursorPos(BufferView const & bv,
539                 CursorSlice const & sl, bool boundary, int & x, int & y) const
540 {
541         x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
542         y = bv.textMetrics(&text_).cursorY(sl, boundary);
543 }
544
545
546 bool InsetText::showInsetDialog(BufferView *) const
547 {
548         return false;
549 }
550
551
552 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
553 {
554         clear();
555         Paragraph & first = paragraphs().front();
556         for (unsigned int i = 0; i < data.length(); ++i)
557                 first.insertChar(i, data[i], font, trackChanges);
558 }
559
560
561 void InsetText::setAutoBreakRows(bool flag)
562 {
563         if (flag == text_.autoBreakRows_)
564                 return;
565
566         text_.autoBreakRows_ = flag;
567         if (flag)
568                 return;
569
570         // remove previously existing newlines
571         ParagraphList::iterator it = paragraphs().begin();
572         ParagraphList::iterator end = paragraphs().end();
573         for (; it != end; ++it)
574                 for (int i = 0; i < it->size(); ++i)
575                         if (it->isNewline(i))
576                                 // do not track the change, because the user
577                                 // is not allowed to revert/reject it
578                                 it->eraseChar(i, false);
579 }
580
581
582 void InsetText::setDrawFrame(bool flag)
583 {
584         drawFrame_ = flag;
585 }
586
587
588 ColorCode InsetText::frameColor() const
589 {
590         return frame_color_;
591 }
592
593
594 void InsetText::setFrameColor(ColorCode col)
595 {
596         frame_color_ = col;
597 }
598
599
600 void InsetText::appendParagraphs(ParagraphList & plist)
601 {
602         // There is little we can do here to keep track of changes.
603         // As of 2006/10/20, appendParagraphs is used exclusively by
604         // LyXTabular::setMultiColumn. In this context, the paragraph break
605         // is lost irreversibly and the appended text doesn't really change
606
607         ParagraphList & pl = paragraphs();
608
609         ParagraphList::iterator pit = plist.begin();
610         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
611         ++pit;
612         mergeParagraph(buffer().params(), pl,
613                        distance(pl.begin(), ins) - 1);
614
615         for_each(pit, plist.end(),
616                  bind(&ParagraphList::push_back, ref(pl), _1));
617 }
618
619
620 void InsetText::addPreview(PreviewLoader & loader) const
621 {
622         ParagraphList::const_iterator pit = paragraphs().begin();
623         ParagraphList::const_iterator pend = paragraphs().end();
624
625         for (; pit != pend; ++pit) {
626                 InsetList::const_iterator it  = pit->insetList().begin();
627                 InsetList::const_iterator end = pit->insetList().end();
628                 for (; it != end; ++it)
629                         it->inset->addPreview(loader);
630         }
631 }
632
633
634 ParagraphList const & InsetText::paragraphs() const
635 {
636         return text_.paragraphs();
637 }
638
639
640 ParagraphList & InsetText::paragraphs()
641 {
642         return text_.paragraphs();
643 }
644
645
646 void InsetText::updateLabels(ParIterator const & it)
647 {
648         ParIterator it2 = it;
649         it2.forwardPos();
650         LASSERT(&it2.inset() == this && it2.pit() == 0, return);
651         if (producesOutput())
652                 buffer().updateLabels(it2);
653         else {
654                 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
655                 Counters const savecnt = tclass.counters();
656                 buffer().updateLabels(it2);
657                 tclass.counters() = savecnt;
658         }
659 }
660
661
662 void InsetText::tocString(odocstream & os) const
663 {
664         if (!getLayout().isInToc())
665                 return;
666         os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
667 }
668
669
670
671 void InsetText::addToToc(DocIterator const & cdit)
672 {
673         DocIterator dit = cdit;
674         dit.push_back(CursorSlice(*this));
675         Toc & toc = buffer().tocBackend().toc("tableofcontents");
676
677         BufferParams const & bufparams = buffer_->params();
678         const int min_toclevel = bufparams.documentClass().min_toclevel();
679
680         // For each paragraph, traverse its insets and let them add
681         // their toc items
682         ParagraphList & pars = paragraphs();
683         pit_type pend = paragraphs().size();
684         for (pit_type pit = 0; pit != pend; ++pit) {
685                 Paragraph const & par = pars[pit];
686                 dit.pit() = pit;
687                 // the string that goes to the toc (could be the optarg)
688                 docstring tocstring;
689                 InsetList::const_iterator it  = par.insetList().begin();
690                 InsetList::const_iterator end = par.insetList().end();
691                 for (; it != end; ++it) {
692                         Inset & inset = *it->inset;
693                         dit.pos() = it->pos;
694                         //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
695                         inset.addToToc(dit);
696                         switch (inset.lyxCode()) {
697                         case OPTARG_CODE: {
698                                 if (!tocstring.empty())
699                                         break;
700                                 dit.pos() = 0;
701                                 Paragraph const & insetpar =
702                                         *static_cast<InsetOptArg&>(inset).paragraphs().begin();
703                                 if (!par.labelString().empty())
704                                         tocstring = par.labelString() + ' ';
705                                 tocstring += insetpar.asString(AS_STR_INSETS);
706                                 break;
707                         }
708                         default:
709                                 break;
710                         }
711                 }
712                 // now the toc entry for the paragraph
713                 int const toclevel = par.layout().toclevel;
714                 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
715                         dit.pos() = 0;
716                         // insert this into the table of contents
717                         if (tocstring.empty())
718                                 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
719                         toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
720                 }
721                 
722                 // And now the list of changes.
723                 par.addChangesToToc(dit, buffer());
724         }
725 }
726
727
728 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
729 {
730         if (buffer().isClean())
731                 return Inset::notifyCursorLeaves(old, cur);
732         
733         // find text inset in old cursor
734         Cursor insetCur = old;
735         int scriptSlice = insetCur.find(this);
736         LASSERT(scriptSlice != -1, /**/);
737         insetCur.cutOff(scriptSlice);
738         LASSERT(&insetCur.inset() == this, /**/);
739         
740         // update the old paragraph's words
741         insetCur.paragraph().updateWords();
742         
743         return Inset::notifyCursorLeaves(old, cur);
744 }
745
746
747 bool InsetText::completionSupported(Cursor const & cur) const
748 {
749         //LASSERT(&cur.bv().cursor().inset() != this, return false);
750         return text_.completionSupported(cur);
751 }
752
753
754 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
755 {
756         return completionSupported(cur);
757 }
758
759
760 bool InsetText::automaticInlineCompletion() const
761 {
762         return lyxrc.completion_inline_text;
763 }
764
765
766 bool InsetText::automaticPopupCompletion() const
767 {
768         return lyxrc.completion_popup_text;
769 }
770
771
772 bool InsetText::showCompletionCursor() const
773 {
774         return lyxrc.completion_cursor_text;
775 }
776
777
778 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
779 {
780         return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
781 }
782
783
784 docstring InsetText::completionPrefix(Cursor const & cur) const
785 {
786         if (!completionSupported(cur))
787                 return docstring();
788         return text_.completionPrefix(cur);
789 }
790
791
792 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
793         bool finished)
794 {
795         if (!completionSupported(cur))
796                 return false;
797
798         return text_.insertCompletion(cur, s, finished);
799 }
800
801
802 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y, 
803         Dimension & dim) const
804 {
805         TextMetrics const & tm = cur.bv().textMetrics(&text_);
806         tm.completionPosAndDim(cur, x, y, dim);
807 }
808
809
810 docstring InsetText::contextMenu(BufferView const &, int, int) const
811 {
812         return from_ascii("context-edit");
813 }
814
815
816 InsetCaption const * InsetText::getCaptionInset() const
817 {
818         ParagraphList::const_iterator pit = paragraphs().begin();
819         for (; pit != paragraphs().end(); ++pit) {
820                 InsetList::const_iterator it = pit->insetList().begin();
821                 for (; it != pit->insetList().end(); ++it) {
822                         Inset & inset = *it->inset;
823                         if (inset.lyxCode() == CAPTION_CODE) {
824                                 InsetCaption const * ins =
825                                         static_cast<InsetCaption const *>(it->inset);
826                                 return ins;
827                         }
828                 }
829         }
830         return 0;
831 }
832
833
834 docstring InsetText::getCaptionText(OutputParams const & runparams) const
835 {
836         InsetCaption const * ins = getCaptionInset();
837         if (ins == 0)
838                 return docstring();
839
840         odocstringstream ods;
841         ins->getCaptionAsPlaintext(ods, runparams);
842         return ods.str();
843 }
844
845
846 docstring InsetText::getCaptionHTML(OutputParams const & runparams) const
847 {
848         InsetCaption const * ins = getCaptionInset();
849         if (ins == 0)
850                 return docstring();
851
852         odocstringstream ods;
853         docstring def = ins->getCaptionAsHTML(ods, runparams);
854         if (!def.empty())
855                 ods << def << '\n';
856         return ods.str();
857 }
858
859
860 } // namespace lyx