3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetText.h"
15 #include "insets/InsetOptArg.h"
17 #include "buffer_funcs.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "CompletionList.h"
22 #include "CoordCache.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"
36 #include "MetricsInfo.h"
37 #include "output_docbook.h"
38 #include "output_latex.h"
39 #include "output_xhtml.h"
40 #include "OutputParams.h"
41 #include "output_plaintext.h"
42 #include "paragraph_funcs.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
49 #include "TextClass.h"
51 #include "TextMetrics.h"
52 #include "TocBackend.h"
54 #include "frontends/alert.h"
55 #include "frontends/Painter.h"
57 #include "support/debug.h"
58 #include "support/gettext.h"
59 #include "support/lstrings.h"
61 #include <boost/bind.hpp>
62 #include "support/lassert.h"
65 using namespace lyx::support;
72 using graphics::PreviewLoader;
75 /////////////////////////////////////////////////////////////////////
77 InsetText::InsetText(Buffer const & buf, UsePlain type)
78 : drawFrame_(false), frame_color_(Color_insetframe)
80 setBuffer(const_cast<Buffer &>(buf));
85 InsetText::InsetText(InsetText const & in)
88 text_.autoBreakRows_ = in.text_.autoBreakRows_;
89 drawFrame_ = in.drawFrame_;
90 frame_color_ = in.frame_color_;
91 text_.paragraphs() = in.text_.paragraphs();
96 void InsetText::setBuffer(Buffer & buf)
98 ParagraphList::iterator end = paragraphs().end();
99 for (ParagraphList::iterator it = paragraphs().begin(); it != end; ++it)
101 Inset::setBuffer(buf);
105 void InsetText::initParagraphs(UsePlain type)
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);
115 ourpar.setPlainLayout(dc);
119 void InsetText::setParagraphOwner()
121 for_each(paragraphs().begin(), paragraphs().end(),
122 bind(&Paragraph::setInsetOwner, _1, this));
126 void InsetText::clear()
128 ParagraphList & pars = paragraphs();
129 LASSERT(!pars.empty(), /**/);
131 // This is a gross hack...
132 Layout const & old_layout = pars.begin()->layout();
135 pars.push_back(Paragraph());
136 pars.begin()->setInsetOwner(this);
137 pars.begin()->setLayout(old_layout);
141 Dimension const InsetText::dimension(BufferView const & bv) const
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;
152 void InsetText::write(ostream & os) const
155 text_.write(buffer(), os);
159 void InsetText::read(Lexer & lex)
163 // delete the initial paragraph
164 Paragraph oldpar = *paragraphs().begin();
165 paragraphs().clear();
167 lex.setContext("InsetText::read");
168 bool res = text_.read(buffer(), lex, errorList, this);
171 lex.printError("Missing \\end_inset at this point. ");
174 // ensure we have at least one paragraph.
175 if (paragraphs().empty())
176 paragraphs().push_back(oldpar);
180 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
182 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
184 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
186 // Hand font through to contained lyxtext:
187 tm.font_.fontInfo() = mi.base.font;
188 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
190 // This can happen when a layout has a left and right margin,
191 // and the view is made very narrow. We can't do better than
192 // to draw it partly out of view (bug 5890).
193 if (mi.base.textwidth < 1)
194 mi.base.textwidth = 1;
197 tm.metrics(mi, dim, mi.base.textwidth);
200 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
201 dim.asc += TEXT_TO_INSET_OFFSET;
202 dim.des += TEXT_TO_INSET_OFFSET;
203 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
207 void InsetText::draw(PainterInfo & pi, int x, int y) const
209 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
211 if (drawFrame_ || pi.full_repaint) {
212 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
213 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
214 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
215 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
217 pi.pain.fillRectangle(xframe, yframe, w, h,
218 pi.backgroundColor(this));
221 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
223 ColorCode const old_color = pi.background_color;
224 pi.background_color = pi.backgroundColor(this, false);
226 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
228 pi.background_color = old_color;
232 docstring InsetText::editMessage() const
234 return _("Opened Text Inset");
238 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
240 pit_type const pit = front ? 0 : paragraphs().size() - 1;
241 pos_type pos = front ? 0 : paragraphs().back().size();
243 // if visual information is not to be ignored, move to extreme right/left
244 if (entry_from != ENTRY_DIRECTION_IGNORE) {
245 Cursor temp_cur = cur;
246 temp_cur.pit() = pit;
247 temp_cur.pos() = pos;
248 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
249 pos = temp_cur.pos();
252 text_.setCursor(cur.top(), pit, pos);
253 cur.clearSelection();
258 Inset * InsetText::editXY(Cursor & cur, int x, int y)
260 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
264 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
266 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
267 << " [ cmd.action = " << cmd.action << ']');
269 switch (cmd.action) {
271 case LFUN_CLIPBOARD_PASTE:
272 case LFUN_SELECTION_PASTE:
273 case LFUN_PRIMARY_SELECTION_PASTE:
274 text_.dispatch(cur, cmd);
275 // If we we can only store plain text, we must reset all
277 // FIXME: Change only the pasted paragraphs
281 text_.dispatch(cur, cmd);
284 if (!cur.result().dispatched())
285 Inset::doDispatch(cur, cmd);
289 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
290 FuncStatus & status) const
292 switch (cmd.action) {
294 status.setEnabled(!forcePlainLayout());
297 case LFUN_LAYOUT_PARAGRAPH:
298 case LFUN_PARAGRAPH_PARAMS:
299 case LFUN_PARAGRAPH_PARAMS_APPLY:
300 case LFUN_PARAGRAPH_SPACING:
301 case LFUN_PARAGRAPH_UPDATE:
302 status.setEnabled(allowParagraphCustomization());
305 // Dispatch only to text_ if the cursor is inside
306 // the text_. It is not for context menus (bug 5797).
308 if (cur.text() == &text_)
309 ret = text_.getStatus(cur, cmd, status);
312 ret = Inset::getStatus(cur, cmd, status);
318 void InsetText::fixParagraphsFont()
320 Font font(inherit_font, buffer().params().language);
321 if (getLayout().isForceLtr())
322 font.setLanguage(latex_language);
323 if (getLayout().isPassThru()) {
324 ParagraphList::iterator par = paragraphs().begin();
325 ParagraphList::iterator const end = paragraphs().end();
327 par->resetFonts(font);
328 par->params().clear();
335 void InsetText::setChange(Change const & change)
337 ParagraphList::iterator pit = paragraphs().begin();
338 ParagraphList::iterator end = paragraphs().end();
339 for (; pit != end; ++pit) {
340 pit->setChange(change);
345 void InsetText::acceptChanges()
347 text_.acceptChanges(buffer().params());
351 void InsetText::rejectChanges()
353 text_.rejectChanges(buffer().params());
357 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
360 latexParagraphs(buffer(), text_, os, texrow, runparams);
361 return texrow.rows();
365 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
367 ParagraphList::const_iterator beg = paragraphs().begin();
368 ParagraphList::const_iterator end = paragraphs().end();
369 ParagraphList::const_iterator it = beg;
370 bool ref_printed = false;
372 for (; it != end; ++it) {
375 if (runparams.linelen > 0)
378 odocstringstream oss;
379 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
380 docstring const str = oss.str();
382 // FIXME: len is not computed fully correctly; in principle,
383 // we have to count the characters after the last '\n'
391 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
393 docbookParagraphs(paragraphs(), buffer(), os, runparams);
398 docstring InsetText::xhtml(odocstream & os, OutputParams const & runparams) const
400 xhtmlParagraphs(paragraphs(), buffer(), os, runparams);
405 void InsetText::validate(LaTeXFeatures & features) const
407 for_each(paragraphs().begin(), paragraphs().end(),
408 bind(&Paragraph::validate, _1, ref(features)));
412 void InsetText::cursorPos(BufferView const & bv,
413 CursorSlice const & sl, bool boundary, int & x, int & y) const
415 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
416 y = bv.textMetrics(&text_).cursorY(sl, boundary);
420 bool InsetText::showInsetDialog(BufferView *) const
426 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
429 Paragraph & first = paragraphs().front();
430 for (unsigned int i = 0; i < data.length(); ++i)
431 first.insertChar(i, data[i], font, trackChanges);
435 void InsetText::setAutoBreakRows(bool flag)
437 if (flag == text_.autoBreakRows_)
440 text_.autoBreakRows_ = flag;
444 // remove previously existing newlines
445 ParagraphList::iterator it = paragraphs().begin();
446 ParagraphList::iterator end = paragraphs().end();
447 for (; it != end; ++it)
448 for (int i = 0; i < it->size(); ++i)
449 if (it->isNewline(i))
450 // do not track the change, because the user
451 // is not allowed to revert/reject it
452 it->eraseChar(i, false);
456 void InsetText::setDrawFrame(bool flag)
462 ColorCode InsetText::frameColor() const
468 void InsetText::setFrameColor(ColorCode col)
474 void InsetText::appendParagraphs(ParagraphList & plist)
476 // There is little we can do here to keep track of changes.
477 // As of 2006/10/20, appendParagraphs is used exclusively by
478 // LyXTabular::setMultiColumn. In this context, the paragraph break
479 // is lost irreversibly and the appended text doesn't really change
481 ParagraphList & pl = paragraphs();
483 ParagraphList::iterator pit = plist.begin();
484 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
486 mergeParagraph(buffer().params(), pl,
487 distance(pl.begin(), ins) - 1);
489 for_each(pit, plist.end(),
490 bind(&ParagraphList::push_back, ref(pl), _1));
494 void InsetText::addPreview(PreviewLoader & loader) const
496 ParagraphList::const_iterator pit = paragraphs().begin();
497 ParagraphList::const_iterator pend = paragraphs().end();
499 for (; pit != pend; ++pit) {
500 InsetList::const_iterator it = pit->insetList().begin();
501 InsetList::const_iterator end = pit->insetList().end();
502 for (; it != end; ++it)
503 it->inset->addPreview(loader);
508 ParagraphList const & InsetText::paragraphs() const
510 return text_.paragraphs();
514 ParagraphList & InsetText::paragraphs()
516 return text_.paragraphs();
520 void InsetText::updateLabels(ParIterator const & it)
522 ParIterator it2 = it;
524 LASSERT(&it2.inset() == this && it2.pit() == 0, return);
525 if (producesOutput())
526 buffer().updateLabels(it2);
528 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
529 Counters const savecnt = tclass.counters();
530 buffer().updateLabels(it2);
531 tclass.counters() = savecnt;
536 void InsetText::addToToc(DocIterator const & cdit)
538 DocIterator dit = cdit;
539 dit.push_back(CursorSlice(*this));
540 Toc & toc = buffer().tocBackend().toc("tableofcontents");
542 BufferParams const & bufparams = buffer_->params();
543 const int min_toclevel = bufparams.documentClass().min_toclevel();
545 // For each paragraph, traverse its insets and let them add
547 ParagraphList & pars = paragraphs();
548 pit_type pend = paragraphs().size();
549 for (pit_type pit = 0; pit != pend; ++pit) {
550 Paragraph const & par = pars[pit];
552 // the string that goes to the toc (could be the optarg)
554 InsetList::const_iterator it = par.insetList().begin();
555 InsetList::const_iterator end = par.insetList().end();
556 for (; it != end; ++it) {
557 Inset & inset = *it->inset;
559 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
561 switch (inset.lyxCode()) {
563 if (!tocstring.empty())
566 Paragraph const & insetpar =
567 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
568 if (!par.labelString().empty())
569 tocstring = par.labelString() + ' ';
570 tocstring += insetpar.asString(AS_STR_INSETS);
577 // now the toc entry for the paragraph
578 int const toclevel = par.layout().toclevel;
579 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
581 // insert this into the table of contents
582 if (tocstring.empty())
583 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
584 toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
587 // And now the list of changes.
588 par.addChangesToToc(dit, buffer());
593 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
595 if (buffer().isClean())
596 return Inset::notifyCursorLeaves(old, cur);
598 // find text inset in old cursor
599 Cursor insetCur = old;
600 int scriptSlice = insetCur.find(this);
601 LASSERT(scriptSlice != -1, /**/);
602 insetCur.cutOff(scriptSlice);
603 LASSERT(&insetCur.inset() == this, /**/);
605 // update the old paragraph's words
606 insetCur.paragraph().updateWords();
608 return Inset::notifyCursorLeaves(old, cur);
612 bool InsetText::completionSupported(Cursor const & cur) const
614 //LASSERT(&cur.bv().cursor().inset() != this, return false);
615 return text_.completionSupported(cur);
619 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
621 return completionSupported(cur);
625 bool InsetText::automaticInlineCompletion() const
627 return lyxrc.completion_inline_text;
631 bool InsetText::automaticPopupCompletion() const
633 return lyxrc.completion_popup_text;
637 bool InsetText::showCompletionCursor() const
639 return lyxrc.completion_cursor_text;
643 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
645 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
649 docstring InsetText::completionPrefix(Cursor const & cur) const
651 if (!completionSupported(cur))
653 return text_.completionPrefix(cur);
657 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
660 if (!completionSupported(cur))
663 return text_.insertCompletion(cur, s, finished);
667 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
668 Dimension & dim) const
670 TextMetrics const & tm = cur.bv().textMetrics(&text_);
671 tm.completionPosAndDim(cur, x, y, dim);
675 docstring InsetText::contextMenu(BufferView const &, int, int) const
677 return from_ascii("context-edit");
681 InsetCaption const * InsetText::getCaptionInset() const
683 ParagraphList::const_iterator pit = paragraphs().begin();
684 for (; pit != paragraphs().end(); ++pit) {
685 InsetList::const_iterator it = pit->insetList().begin();
686 for (; it != pit->insetList().end(); ++it) {
687 Inset & inset = *it->inset;
688 if (inset.lyxCode() == CAPTION_CODE) {
689 InsetCaption const * ins =
690 static_cast<InsetCaption const *>(it->inset);
699 docstring InsetText::getCaptionText(OutputParams const & runparams) const
701 InsetCaption const * ins = getCaptionInset();
705 odocstringstream ods;
706 ins->getCaptionAsPlaintext(ods, runparams);
711 docstring InsetText::getCaptionHTML(OutputParams const & runparams) const
713 InsetCaption const * ins = getCaptionInset();
717 odocstringstream ods;
718 docstring def = ins->getCaptionAsHTML(ods, runparams);