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 "InsetList.h"
33 #include "MetricsInfo.h"
34 #include "output_docbook.h"
35 #include "output_latex.h"
36 #include "OutputParams.h"
37 #include "output_plaintext.h"
38 #include "paragraph_funcs.h"
39 #include "Paragraph.h"
40 #include "ParagraphParameters.h"
41 #include "ParIterator.h"
45 #include "TextClass.h"
47 #include "TextMetrics.h"
48 #include "TocBackend.h"
50 #include "frontends/alert.h"
51 #include "frontends/Painter.h"
53 #include "support/debug.h"
54 #include "support/gettext.h"
55 #include "support/lstrings.h"
57 #include <boost/bind.hpp>
58 #include "support/lassert.h"
61 using namespace lyx::support;
68 using graphics::PreviewLoader;
71 /////////////////////////////////////////////////////////////////////
73 InsetText::InsetText(Buffer const & buf)
74 : drawFrame_(false), frame_color_(Color_insetframe)
76 initParagraphs(buf.params());
77 setBuffer(const_cast<Buffer &>(buf));
81 InsetText::InsetText(InsetText const & in)
84 text_.autoBreakRows_ = in.text_.autoBreakRows_;
85 drawFrame_ = in.drawFrame_;
86 frame_color_ = in.frame_color_;
87 text_.paragraphs() = in.text_.paragraphs();
92 void InsetText::setBuffer(Buffer & buf)
94 ParagraphList::iterator end = paragraphs().end();
95 for (ParagraphList::iterator it = paragraphs().begin(); it != end; ++it)
97 Inset::setBuffer(buf);
101 void InsetText::initParagraphs(BufferParams const & bparams)
103 LASSERT(paragraphs().empty(), /**/);
104 paragraphs().push_back(Paragraph());
105 Paragraph & ourpar = paragraphs().back();
106 ourpar.setInsetOwner(this);
107 ourpar.setPlainOrDefaultLayout(bparams.documentClass());
111 void InsetText::setParagraphOwner()
113 for_each(paragraphs().begin(), paragraphs().end(),
114 bind(&Paragraph::setInsetOwner, _1, this));
118 void InsetText::clear()
120 ParagraphList & pars = paragraphs();
121 LASSERT(!pars.empty(), /**/);
123 // This is a gross hack...
124 Layout const & old_layout = pars.begin()->layout();
127 pars.push_back(Paragraph());
128 pars.begin()->setInsetOwner(this);
129 pars.begin()->setLayout(old_layout);
133 Dimension const InsetText::dimension(BufferView const & bv) const
135 TextMetrics const & tm = bv.textMetrics(&text_);
136 Dimension dim = tm.dimension();
137 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
138 dim.des += TEXT_TO_INSET_OFFSET;
139 dim.asc += TEXT_TO_INSET_OFFSET;
144 void InsetText::write(ostream & os) const
147 text_.write(buffer(), os);
151 void InsetText::read(Lexer & lex)
155 // delete the initial paragraph
156 Paragraph oldpar = *paragraphs().begin();
157 paragraphs().clear();
159 lex.setContext("InsetText::read");
160 bool res = text_.read(buffer(), lex, errorList, this);
163 lex.printError("Missing \\end_inset at this point. ");
166 // ensure we have at least one paragraph.
167 if (paragraphs().empty())
168 paragraphs().push_back(oldpar);
172 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
174 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
176 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
178 // Hand font through to contained lyxtext:
179 tm.font_.fontInfo() = mi.base.font;
180 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
182 tm.metrics(mi, dim, mi.base.textwidth);
185 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
186 dim.asc += TEXT_TO_INSET_OFFSET;
187 dim.des += TEXT_TO_INSET_OFFSET;
188 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
192 void InsetText::draw(PainterInfo & pi, int x, int y) const
194 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
196 if (drawFrame_ || pi.full_repaint) {
197 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
198 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
199 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
200 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
202 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
204 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
206 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
210 docstring InsetText::editMessage() const
212 return _("Opened Text Inset");
216 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
218 pit_type const pit = front ? 0 : paragraphs().size() - 1;
219 pos_type pos = front ? 0 : paragraphs().back().size();
221 // if visual information is not to be ignored, move to extreme right/left
222 if (entry_from != ENTRY_DIRECTION_IGNORE) {
223 Cursor temp_cur = cur;
224 temp_cur.pit() = pit;
225 temp_cur.pos() = pos;
226 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
227 pos = temp_cur.pos();
230 text_.setCursor(cur.top(), pit, pos);
231 cur.clearSelection();
236 Inset * InsetText::editXY(Cursor & cur, int x, int y)
238 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
242 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
244 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
245 << " [ cmd.action = " << cmd.action << ']');
246 text_.dispatch(cur, cmd);
250 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
251 FuncStatus & status) const
253 return text_.getStatus(cur, cmd, status);
257 void InsetText::setChange(Change const & change)
259 ParagraphList::iterator pit = paragraphs().begin();
260 ParagraphList::iterator end = paragraphs().end();
261 for (; pit != end; ++pit) {
262 pit->setChange(change);
267 void InsetText::acceptChanges(BufferParams const & bparams)
269 text_.acceptChanges(bparams);
273 void InsetText::rejectChanges(BufferParams const & bparams)
275 text_.rejectChanges(bparams);
279 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
282 latexParagraphs(buffer(), text_, os, texrow, runparams);
283 return texrow.rows();
287 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
289 ParagraphList::const_iterator beg = paragraphs().begin();
290 ParagraphList::const_iterator end = paragraphs().end();
291 ParagraphList::const_iterator it = beg;
292 bool ref_printed = false;
294 for (; it != end; ++it) {
297 if (runparams.linelen > 0)
300 odocstringstream oss;
301 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
302 docstring const str = oss.str();
304 // FIXME: len is not computed fully correctly; in principle,
305 // we have to count the characters after the last '\n'
313 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
315 docbookParagraphs(paragraphs(), buffer(), os, runparams);
320 void InsetText::validate(LaTeXFeatures & features) const
322 for_each(paragraphs().begin(), paragraphs().end(),
323 bind(&Paragraph::validate, _1, ref(features)));
327 void InsetText::cursorPos(BufferView const & bv,
328 CursorSlice const & sl, bool boundary, int & x, int & y) const
330 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
331 y = bv.textMetrics(&text_).cursorY(sl, boundary);
335 bool InsetText::showInsetDialog(BufferView *) const
341 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
344 Paragraph & first = paragraphs().front();
345 for (unsigned int i = 0; i < data.length(); ++i)
346 first.insertChar(i, data[i], font, trackChanges);
350 void InsetText::setAutoBreakRows(bool flag)
352 if (flag == text_.autoBreakRows_)
355 text_.autoBreakRows_ = flag;
359 // remove previously existing newlines
360 ParagraphList::iterator it = paragraphs().begin();
361 ParagraphList::iterator end = paragraphs().end();
362 for (; it != end; ++it)
363 for (int i = 0; i < it->size(); ++i)
364 if (it->isNewline(i))
365 // do not track the change, because the user
366 // is not allowed to revert/reject it
367 it->eraseChar(i, false);
371 void InsetText::setDrawFrame(bool flag)
377 ColorCode InsetText::frameColor() const
383 void InsetText::setFrameColor(ColorCode col)
389 void InsetText::appendParagraphs(ParagraphList & plist)
391 // There is little we can do here to keep track of changes.
392 // As of 2006/10/20, appendParagraphs is used exclusively by
393 // LyXTabular::setMultiColumn. In this context, the paragraph break
394 // is lost irreversibly and the appended text doesn't really change
396 ParagraphList & pl = paragraphs();
398 ParagraphList::iterator pit = plist.begin();
399 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
401 mergeParagraph(buffer().params(), pl,
402 distance(pl.begin(), ins) - 1);
404 for_each(pit, plist.end(),
405 bind(&ParagraphList::push_back, ref(pl), _1));
409 void InsetText::addPreview(PreviewLoader & loader) const
411 ParagraphList::const_iterator pit = paragraphs().begin();
412 ParagraphList::const_iterator pend = paragraphs().end();
414 for (; pit != pend; ++pit) {
415 InsetList::const_iterator it = pit->insetList().begin();
416 InsetList::const_iterator end = pit->insetList().end();
417 for (; it != end; ++it)
418 it->inset->addPreview(loader);
423 ParagraphList const & InsetText::paragraphs() const
425 return text_.paragraphs();
429 ParagraphList & InsetText::paragraphs()
431 return text_.paragraphs();
435 void InsetText::updateLabels(ParIterator const & it)
437 ParIterator it2 = it;
439 LASSERT(&it2.inset() == this && it2.pit() == 0, /**/);
440 if (producesOutput())
441 lyx::updateLabels(buffer(), it2);
443 DocumentClass const & tclass = buffer().params().documentClass();
444 Counters const savecnt = tclass.counters();
445 lyx::updateLabels(buffer(), it2);
446 tclass.counters() = savecnt;
451 void InsetText::addToToc(DocIterator const & cdit)
453 DocIterator dit = cdit;
454 dit.push_back(CursorSlice(*this));
455 Toc & toc = buffer().tocBackend().toc("tableofcontents");
457 BufferParams const & bufparams = buffer_->params();
458 const int min_toclevel = bufparams.documentClass().min_toclevel();
460 // For each paragraph, traverse its insets and let them add
462 ParagraphList & pars = paragraphs();
463 pit_type pend = paragraphs().size();
464 for (pit_type pit = 0; pit != pend; ++pit) {
465 Paragraph const & par = pars[pit];
467 // the string that goes to the toc (could be the optarg)
469 InsetList::const_iterator it = par.insetList().begin();
470 InsetList::const_iterator end = par.insetList().end();
471 for (; it != end; ++it) {
472 Inset & inset = *it->inset;
474 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
476 switch (inset.lyxCode()) {
478 if (!tocstring.empty())
481 Paragraph const & insetpar =
482 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
483 if (!par.labelString().empty())
484 tocstring = par.labelString() + ' ';
485 tocstring += insetpar.asString();
492 /// now the toc entry for the paragraph
493 int const toclevel = par.layout().toclevel;
494 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
496 // insert this into the table of contents
497 if (tocstring.empty())
498 tocstring = par.asString(AS_STR_LABEL);
499 toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
502 // And now the list of changes.
503 par.addChangesToToc(dit, buffer());
508 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
510 if (cur.buffer().isClean())
511 return Inset::notifyCursorLeaves(old, cur);
513 // find text inset in old cursor
514 Cursor insetCur = old;
515 int scriptSlice = insetCur.find(this);
516 LASSERT(scriptSlice != -1, /**/);
517 insetCur.cutOff(scriptSlice);
518 LASSERT(&insetCur.inset() == this, /**/);
520 // update the old paragraph's words
521 insetCur.paragraph().updateWords(insetCur.top());
523 return Inset::notifyCursorLeaves(old, cur);
527 bool InsetText::completionSupported(Cursor const & cur) const
529 Cursor const & bvCur = cur.bv().cursor();
530 if (&bvCur.inset() != this)
532 return text_.completionSupported(cur);
536 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
538 return completionSupported(cur);
542 bool InsetText::automaticInlineCompletion() const
544 return lyxrc.completion_inline_text;
548 bool InsetText::automaticPopupCompletion() const
550 return lyxrc.completion_popup_text;
554 bool InsetText::showCompletionCursor() const
556 return lyxrc.completion_cursor_text;
560 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
562 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
566 docstring InsetText::completionPrefix(Cursor const & cur) const
568 if (!completionSupported(cur))
570 return text_.completionPrefix(cur);
574 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
577 if (!completionSupported(cur))
580 return text_.insertCompletion(cur, s, finished);
584 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
585 Dimension & dim) const
587 TextMetrics const & tm = cur.bv().textMetrics(&text_);
588 tm.completionPosAndDim(cur, x, y, dim);
592 docstring InsetText::contextMenu(BufferView const &, int, int) const
594 return from_ascii("context-edit");