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)
80 InsetText::InsetText(InsetText const & in)
83 text_.autoBreakRows_ = in.text_.autoBreakRows_;
84 drawFrame_ = in.drawFrame_;
85 frame_color_ = in.frame_color_;
86 text_.paragraphs() = in.text_.paragraphs();
91 InsetText::InsetText()
95 void InsetText::initParagraphs(Buffer const & buf)
97 LASSERT(paragraphs().empty(), /**/);
98 buffer_ = const_cast<Buffer *>(&buf);
99 paragraphs().push_back(Paragraph());
100 Paragraph & ourpar = paragraphs().back();
101 ourpar.setEmptyOrDefaultLayout(buf.params().documentClass());
102 ourpar.setInsetOwner(this);
105 void InsetText::setParagraphOwner()
107 for_each(paragraphs().begin(), paragraphs().end(),
108 bind(&Paragraph::setInsetOwner, _1, this));
112 void InsetText::clear()
114 ParagraphList & pars = paragraphs();
115 LASSERT(!pars.empty(), /**/);
117 // This is a gross hack...
118 Layout const & old_layout = pars.begin()->layout();
121 pars.push_back(Paragraph());
122 pars.begin()->setInsetOwner(this);
123 pars.begin()->setLayout(old_layout);
127 Dimension const InsetText::dimension(BufferView const & bv) const
129 TextMetrics const & tm = bv.textMetrics(&text_);
130 Dimension dim = tm.dimension();
131 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
132 dim.des += TEXT_TO_INSET_OFFSET;
133 dim.asc += TEXT_TO_INSET_OFFSET;
138 void InsetText::write(ostream & os) const
141 text_.write(buffer(), os);
145 void InsetText::read(Lexer & lex)
149 // delete the initial paragraph
150 Paragraph oldpar = *paragraphs().begin();
151 paragraphs().clear();
153 lex.setContext("InsetText::read");
154 bool res = text_.read(buffer(), lex, errorList, this);
157 lex.printError("Missing \\end_inset at this point. ");
160 // ensure we have at least one paragraph.
161 if (paragraphs().empty())
162 paragraphs().push_back(oldpar);
166 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
168 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
170 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
172 // Hand font through to contained lyxtext:
173 tm.font_.fontInfo() = mi.base.font;
174 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
176 tm.metrics(mi, dim, mi.base.textwidth);
179 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
180 dim.asc += TEXT_TO_INSET_OFFSET;
181 dim.des += TEXT_TO_INSET_OFFSET;
182 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
186 void InsetText::draw(PainterInfo & pi, int x, int y) const
188 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
190 if (drawFrame_ || pi.full_repaint) {
191 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
192 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
193 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
194 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
196 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
198 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
200 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
204 docstring InsetText::editMessage() const
206 return _("Opened Text Inset");
210 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
212 pit_type const pit = front ? 0 : paragraphs().size() - 1;
213 pos_type pos = front ? 0 : paragraphs().back().size();
215 // if visual information is not to be ignored, move to extreme right/left
216 if (entry_from != ENTRY_DIRECTION_IGNORE) {
217 Cursor temp_cur = cur;
218 temp_cur.pit() = pit;
219 temp_cur.pos() = pos;
220 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
221 pos = temp_cur.pos();
224 text_.setCursor(cur.top(), pit, pos);
225 cur.clearSelection();
230 Inset * InsetText::editXY(Cursor & cur, int x, int y)
232 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
236 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
238 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
239 << " [ cmd.action = " << cmd.action << ']');
240 text_.dispatch(cur, cmd);
244 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
245 FuncStatus & status) const
247 return text_.getStatus(cur, cmd, status);
251 void InsetText::setChange(Change const & change)
253 ParagraphList::iterator pit = paragraphs().begin();
254 ParagraphList::iterator end = paragraphs().end();
255 for (; pit != end; ++pit) {
256 pit->setChange(change);
261 void InsetText::acceptChanges(BufferParams const & bparams)
263 text_.acceptChanges(bparams);
267 void InsetText::rejectChanges(BufferParams const & bparams)
269 text_.rejectChanges(bparams);
273 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
276 latexParagraphs(buffer(), text_, os, texrow, runparams);
277 return texrow.rows();
281 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
283 ParagraphList::const_iterator beg = paragraphs().begin();
284 ParagraphList::const_iterator end = paragraphs().end();
285 ParagraphList::const_iterator it = beg;
286 bool ref_printed = false;
288 for (; it != end; ++it) {
291 if (runparams.linelen > 0)
294 odocstringstream oss;
295 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
296 docstring const str = oss.str();
298 // FIXME: len is not computed fully correctly; in principle,
299 // we have to count the characters after the last '\n'
307 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
309 docbookParagraphs(paragraphs(), buffer(), os, runparams);
314 void InsetText::validate(LaTeXFeatures & features) const
316 for_each(paragraphs().begin(), paragraphs().end(),
317 bind(&Paragraph::validate, _1, ref(features)));
321 void InsetText::cursorPos(BufferView const & bv,
322 CursorSlice const & sl, bool boundary, int & x, int & y) const
324 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
325 y = bv.textMetrics(&text_).cursorY(sl, boundary);
329 bool InsetText::showInsetDialog(BufferView *) const
335 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
338 Paragraph & first = paragraphs().front();
339 for (unsigned int i = 0; i < data.length(); ++i)
340 first.insertChar(i, data[i], font, trackChanges);
344 void InsetText::setAutoBreakRows(bool flag)
346 if (flag == text_.autoBreakRows_)
349 text_.autoBreakRows_ = flag;
353 // remove previously existing newlines
354 ParagraphList::iterator it = paragraphs().begin();
355 ParagraphList::iterator end = paragraphs().end();
356 for (; it != end; ++it)
357 for (int i = 0; i < it->size(); ++i)
358 if (it->isNewline(i))
359 // do not track the change, because the user
360 // is not allowed to revert/reject it
361 it->eraseChar(i, false);
365 void InsetText::setDrawFrame(bool flag)
371 ColorCode InsetText::frameColor() const
377 void InsetText::setFrameColor(ColorCode col)
383 void InsetText::appendParagraphs(ParagraphList & plist)
385 // There is little we can do here to keep track of changes.
386 // As of 2006/10/20, appendParagraphs is used exclusively by
387 // LyXTabular::setMultiColumn. In this context, the paragraph break
388 // is lost irreversibly and the appended text doesn't really change
390 ParagraphList & pl = paragraphs();
392 ParagraphList::iterator pit = plist.begin();
393 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
395 mergeParagraph(buffer().params(), pl,
396 distance(pl.begin(), ins) - 1);
398 for_each(pit, plist.end(),
399 bind(&ParagraphList::push_back, ref(pl), _1));
403 void InsetText::addPreview(PreviewLoader & loader) const
405 ParagraphList::const_iterator pit = paragraphs().begin();
406 ParagraphList::const_iterator pend = paragraphs().end();
408 for (; pit != pend; ++pit) {
409 InsetList::const_iterator it = pit->insetList().begin();
410 InsetList::const_iterator end = pit->insetList().end();
411 for (; it != end; ++it)
412 it->inset->addPreview(loader);
417 ParagraphList const & InsetText::paragraphs() const
419 return text_.paragraphs();
423 ParagraphList & InsetText::paragraphs()
425 return text_.paragraphs();
429 void InsetText::updateLabels(ParIterator const & it)
431 ParIterator it2 = it;
433 LASSERT(&it2.inset() == this && it2.pit() == 0, /**/);
434 if (producesOutput())
435 lyx::updateLabels(buffer(), it2);
437 DocumentClass const & tclass = buffer().params().documentClass();
438 Counters const savecnt = tclass.counters();
439 lyx::updateLabels(buffer(), it2);
440 tclass.counters() = savecnt;
445 void InsetText::addToToc(DocIterator const & cdit)
447 DocIterator dit = cdit;
448 dit.push_back(CursorSlice(*this));
449 Toc & toc = buffer().tocBackend().toc("tableofcontents");
451 BufferParams const & bufparams = buffer_->params();
452 const int min_toclevel = bufparams.documentClass().min_toclevel();
454 // For each paragraph, traverse its insets and let them add
456 ParagraphList & pars = paragraphs();
457 pit_type pend = paragraphs().size();
458 for (pit_type pit = 0; pit != pend; ++pit) {
459 Paragraph const & par = pars[pit];
461 // the string that goes to the toc (could be the optarg)
463 InsetList::const_iterator it = par.insetList().begin();
464 InsetList::const_iterator end = par.insetList().end();
465 for (; it != end; ++it) {
466 Inset & inset = *it->inset;
468 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
470 switch (inset.lyxCode()) {
472 if (!tocstring.empty())
475 Paragraph const & insetpar =
476 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
477 if (!par.labelString().empty())
478 tocstring = par.labelString() + ' ';
479 tocstring += insetpar.asString();
486 /// now the toc entry for the paragraph
487 int const toclevel = par.layout().toclevel;
488 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
490 // insert this into the table of contents
491 if (tocstring.empty())
492 tocstring = par.asString(AS_STR_LABEL);
493 toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
499 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
501 if (cur.buffer().isClean())
502 return Inset::notifyCursorLeaves(old, cur);
504 // find text inset in old cursor
505 Cursor insetCur = old;
506 int scriptSlice = insetCur.find(this);
507 LASSERT(scriptSlice != -1, /**/);
508 insetCur.cutOff(scriptSlice);
509 LASSERT(&insetCur.inset() == this, /**/);
511 // update the old paragraph's words
512 insetCur.paragraph().updateWords(insetCur.top());
514 return Inset::notifyCursorLeaves(old, cur);
518 bool InsetText::completionSupported(Cursor const & cur) const
520 Cursor const & bvCur = cur.bv().cursor();
521 if (&bvCur.inset() != this)
523 return text_.completionSupported(cur);
527 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
529 return completionSupported(cur);
533 bool InsetText::automaticInlineCompletion() const
535 return lyxrc.completion_inline_text;
539 bool InsetText::automaticPopupCompletion() const
541 return lyxrc.completion_popup_text;
545 bool InsetText::showCompletionCursor() const
547 return lyxrc.completion_cursor_text;
551 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
553 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
557 docstring InsetText::completionPrefix(Cursor const & cur) const
559 if (!completionSupported(cur))
561 return text_.completionPrefix(cur);
565 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
568 if (!completionSupported(cur))
571 return text_.insertCompletion(cur, s, finished);
575 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
576 Dimension & dim) const
578 TextMetrics const & tm = cur.bv().textMetrics(&text_);
579 tm.completionPosAndDim(cur, x, y, dim);
583 docstring InsetText::contextMenu(BufferView const &, int, int) const
585 return from_ascii("context-edit");