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