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 "InsetList.h"
34 #include "MetricsInfo.h"
35 #include "output_docbook.h"
36 #include "output_latex.h"
37 #include "OutputParams.h"
38 #include "output_plaintext.h"
39 #include "paragraph_funcs.h"
40 #include "Paragraph.h"
41 #include "ParagraphParameters.h"
42 #include "ParIterator.h"
46 #include "TextClass.h"
48 #include "TextMetrics.h"
49 #include "TocBackend.h"
51 #include "frontends/alert.h"
52 #include "frontends/Painter.h"
54 #include "support/debug.h"
55 #include "support/gettext.h"
56 #include "support/lstrings.h"
58 #include <boost/bind.hpp>
59 #include "support/lassert.h"
62 using namespace lyx::support;
69 using graphics::PreviewLoader;
72 /////////////////////////////////////////////////////////////////////
74 InsetText::InsetText(Buffer const & buf)
75 : drawFrame_(false), frame_color_(Color_insetframe)
77 initParagraphs(buf.params());
78 setBuffer(const_cast<Buffer &>(buf));
82 InsetText::InsetText(InsetText const & in)
85 text_.autoBreakRows_ = in.text_.autoBreakRows_;
86 drawFrame_ = in.drawFrame_;
87 frame_color_ = in.frame_color_;
88 text_.paragraphs() = in.text_.paragraphs();
93 void InsetText::setBuffer(Buffer & buf)
95 ParagraphList::iterator end = paragraphs().end();
96 for (ParagraphList::iterator it = paragraphs().begin(); it != end; ++it)
98 Inset::setBuffer(buf);
102 void InsetText::initParagraphs(BufferParams const & bparams)
104 LASSERT(paragraphs().empty(), /**/);
105 paragraphs().push_back(Paragraph());
106 Paragraph & ourpar = paragraphs().back();
107 ourpar.setInsetOwner(this);
108 ourpar.setPlainOrDefaultLayout(bparams.documentClass());
112 void InsetText::setParagraphOwner()
114 for_each(paragraphs().begin(), paragraphs().end(),
115 bind(&Paragraph::setInsetOwner, _1, this));
119 void InsetText::clear()
121 ParagraphList & pars = paragraphs();
122 LASSERT(!pars.empty(), /**/);
124 // This is a gross hack...
125 Layout const & old_layout = pars.begin()->layout();
128 pars.push_back(Paragraph());
129 pars.begin()->setInsetOwner(this);
130 pars.begin()->setLayout(old_layout);
134 Dimension const InsetText::dimension(BufferView const & bv) const
136 TextMetrics const & tm = bv.textMetrics(&text_);
137 Dimension dim = tm.dimension();
138 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
139 dim.des += TEXT_TO_INSET_OFFSET;
140 dim.asc += TEXT_TO_INSET_OFFSET;
145 void InsetText::write(ostream & os) const
148 text_.write(buffer(), os);
152 void InsetText::read(Lexer & lex)
156 // delete the initial paragraph
157 Paragraph oldpar = *paragraphs().begin();
158 paragraphs().clear();
160 lex.setContext("InsetText::read");
161 bool res = text_.read(buffer(), lex, errorList, this);
164 lex.printError("Missing \\end_inset at this point. ");
167 // ensure we have at least one paragraph.
168 if (paragraphs().empty())
169 paragraphs().push_back(oldpar);
173 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
175 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
177 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
179 // Hand font through to contained lyxtext:
180 tm.font_.fontInfo() = mi.base.font;
181 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
183 tm.metrics(mi, dim, mi.base.textwidth);
186 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
187 dim.asc += TEXT_TO_INSET_OFFSET;
188 dim.des += TEXT_TO_INSET_OFFSET;
189 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
193 void InsetText::draw(PainterInfo & pi, int x, int y) const
195 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
197 if (drawFrame_ || pi.full_repaint) {
198 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
199 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
200 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
201 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
203 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
205 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
207 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
211 docstring InsetText::editMessage() const
213 return _("Opened Text Inset");
217 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
219 pit_type const pit = front ? 0 : paragraphs().size() - 1;
220 pos_type pos = front ? 0 : paragraphs().back().size();
222 // if visual information is not to be ignored, move to extreme right/left
223 if (entry_from != ENTRY_DIRECTION_IGNORE) {
224 Cursor temp_cur = cur;
225 temp_cur.pit() = pit;
226 temp_cur.pos() = pos;
227 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
228 pos = temp_cur.pos();
231 text_.setCursor(cur.top(), pit, pos);
232 cur.clearSelection();
237 Inset * InsetText::editXY(Cursor & cur, int x, int y)
239 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
243 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
245 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
246 << " [ cmd.action = " << cmd.action << ']');
247 text_.dispatch(cur, cmd);
251 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
252 FuncStatus & status) const
254 switch (cmd.action) {
256 status.setEnabled(!forcePlainLayout());
259 case LFUN_LAYOUT_PARAGRAPH:
260 case LFUN_PARAGRAPH_PARAMS:
261 case LFUN_PARAGRAPH_PARAMS_APPLY:
262 case LFUN_PARAGRAPH_SPACING:
263 case LFUN_PARAGRAPH_UPDATE:
264 status.setEnabled(allowParagraphCustomization());
267 return text_.getStatus(cur, cmd, status);
272 void InsetText::setChange(Change const & change)
274 ParagraphList::iterator pit = paragraphs().begin();
275 ParagraphList::iterator end = paragraphs().end();
276 for (; pit != end; ++pit) {
277 pit->setChange(change);
282 void InsetText::acceptChanges(BufferParams const & bparams)
284 text_.acceptChanges(bparams);
288 void InsetText::rejectChanges(BufferParams const & bparams)
290 text_.rejectChanges(bparams);
294 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
297 latexParagraphs(buffer(), text_, os, texrow, runparams);
298 return texrow.rows();
302 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
304 ParagraphList::const_iterator beg = paragraphs().begin();
305 ParagraphList::const_iterator end = paragraphs().end();
306 ParagraphList::const_iterator it = beg;
307 bool ref_printed = false;
309 for (; it != end; ++it) {
312 if (runparams.linelen > 0)
315 odocstringstream oss;
316 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
317 docstring const str = oss.str();
319 // FIXME: len is not computed fully correctly; in principle,
320 // we have to count the characters after the last '\n'
328 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
330 docbookParagraphs(paragraphs(), buffer(), os, runparams);
335 void InsetText::validate(LaTeXFeatures & features) const
337 for_each(paragraphs().begin(), paragraphs().end(),
338 bind(&Paragraph::validate, _1, ref(features)));
342 void InsetText::cursorPos(BufferView const & bv,
343 CursorSlice const & sl, bool boundary, int & x, int & y) const
345 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
346 y = bv.textMetrics(&text_).cursorY(sl, boundary);
350 bool InsetText::showInsetDialog(BufferView *) const
356 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
359 Paragraph & first = paragraphs().front();
360 for (unsigned int i = 0; i < data.length(); ++i)
361 first.insertChar(i, data[i], font, trackChanges);
365 void InsetText::setAutoBreakRows(bool flag)
367 if (flag == text_.autoBreakRows_)
370 text_.autoBreakRows_ = flag;
374 // remove previously existing newlines
375 ParagraphList::iterator it = paragraphs().begin();
376 ParagraphList::iterator end = paragraphs().end();
377 for (; it != end; ++it)
378 for (int i = 0; i < it->size(); ++i)
379 if (it->isNewline(i))
380 // do not track the change, because the user
381 // is not allowed to revert/reject it
382 it->eraseChar(i, false);
386 void InsetText::setDrawFrame(bool flag)
392 ColorCode InsetText::frameColor() const
398 void InsetText::setFrameColor(ColorCode col)
404 void InsetText::appendParagraphs(ParagraphList & plist)
406 // There is little we can do here to keep track of changes.
407 // As of 2006/10/20, appendParagraphs is used exclusively by
408 // LyXTabular::setMultiColumn. In this context, the paragraph break
409 // is lost irreversibly and the appended text doesn't really change
411 ParagraphList & pl = paragraphs();
413 ParagraphList::iterator pit = plist.begin();
414 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
416 mergeParagraph(buffer().params(), pl,
417 distance(pl.begin(), ins) - 1);
419 for_each(pit, plist.end(),
420 bind(&ParagraphList::push_back, ref(pl), _1));
424 void InsetText::addPreview(PreviewLoader & loader) const
426 ParagraphList::const_iterator pit = paragraphs().begin();
427 ParagraphList::const_iterator pend = paragraphs().end();
429 for (; pit != pend; ++pit) {
430 InsetList::const_iterator it = pit->insetList().begin();
431 InsetList::const_iterator end = pit->insetList().end();
432 for (; it != end; ++it)
433 it->inset->addPreview(loader);
438 ParagraphList const & InsetText::paragraphs() const
440 return text_.paragraphs();
444 ParagraphList & InsetText::paragraphs()
446 return text_.paragraphs();
450 void InsetText::updateLabels(ParIterator const & it)
452 ParIterator it2 = it;
454 LASSERT(&it2.inset() == this && it2.pit() == 0, /**/);
455 if (producesOutput())
456 lyx::updateLabels(buffer(), it2);
458 DocumentClass const & tclass = buffer().params().documentClass();
459 Counters const savecnt = tclass.counters();
460 lyx::updateLabels(buffer(), it2);
461 tclass.counters() = savecnt;
466 void InsetText::addToToc(DocIterator const & cdit)
468 DocIterator dit = cdit;
469 dit.push_back(CursorSlice(*this));
470 Toc & toc = buffer().tocBackend().toc("tableofcontents");
472 BufferParams const & bufparams = buffer_->params();
473 const int min_toclevel = bufparams.documentClass().min_toclevel();
475 // For each paragraph, traverse its insets and let them add
477 ParagraphList & pars = paragraphs();
478 pit_type pend = paragraphs().size();
479 for (pit_type pit = 0; pit != pend; ++pit) {
480 Paragraph const & par = pars[pit];
482 // the string that goes to the toc (could be the optarg)
484 InsetList::const_iterator it = par.insetList().begin();
485 InsetList::const_iterator end = par.insetList().end();
486 for (; it != end; ++it) {
487 Inset & inset = *it->inset;
489 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
491 switch (inset.lyxCode()) {
493 if (!tocstring.empty())
496 Paragraph const & insetpar =
497 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
498 if (!par.labelString().empty())
499 tocstring = par.labelString() + ' ';
500 tocstring += insetpar.asString();
507 /// now the toc entry for the paragraph
508 int const toclevel = par.layout().toclevel;
509 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
511 // insert this into the table of contents
512 if (tocstring.empty())
513 tocstring = par.asString(AS_STR_LABEL);
514 toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
517 // And now the list of changes.
518 par.addChangesToToc(dit, buffer());
523 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
525 if (cur.buffer().isClean())
526 return Inset::notifyCursorLeaves(old, cur);
528 // find text inset in old cursor
529 Cursor insetCur = old;
530 int scriptSlice = insetCur.find(this);
531 LASSERT(scriptSlice != -1, /**/);
532 insetCur.cutOff(scriptSlice);
533 LASSERT(&insetCur.inset() == this, /**/);
535 // update the old paragraph's words
536 insetCur.paragraph().updateWords(insetCur.top());
538 return Inset::notifyCursorLeaves(old, cur);
542 bool InsetText::completionSupported(Cursor const & cur) const
544 Cursor const & bvCur = cur.bv().cursor();
545 if (&bvCur.inset() != this)
547 return text_.completionSupported(cur);
551 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
553 return completionSupported(cur);
557 bool InsetText::automaticInlineCompletion() const
559 return lyxrc.completion_inline_text;
563 bool InsetText::automaticPopupCompletion() const
565 return lyxrc.completion_popup_text;
569 bool InsetText::showCompletionCursor() const
571 return lyxrc.completion_cursor_text;
575 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
577 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
581 docstring InsetText::completionPrefix(Cursor const & cur) const
583 if (!completionSupported(cur))
585 return text_.completionPrefix(cur);
589 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
592 if (!completionSupported(cur))
595 return text_.insertCompletion(cur, s, finished);
599 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
600 Dimension & dim) const
602 TextMetrics const & tm = cur.bv().textMetrics(&text_);
603 tm.completionPosAndDim(cur, x, y, dim);
607 docstring InsetText::contextMenu(BufferView const &, int, int) const
609 return from_ascii("context-edit");