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, UsePlain type)
75 : drawFrame_(false), frame_color_(Color_insetframe)
77 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(UsePlain type)
104 LASSERT(paragraphs().empty(), /**/);
105 paragraphs().push_back(Paragraph());
106 Paragraph & ourpar = paragraphs().back();
107 ourpar.setInsetOwner(this);
108 DocumentClass const & dc = buffer_->params().documentClass();
109 if (type == DefaultLayout)
110 ourpar.setDefaultLayout(dc);
112 ourpar.setPlainLayout(dc);
116 void InsetText::setParagraphOwner()
118 for_each(paragraphs().begin(), paragraphs().end(),
119 bind(&Paragraph::setInsetOwner, _1, this));
123 void InsetText::clear()
125 ParagraphList & pars = paragraphs();
126 LASSERT(!pars.empty(), /**/);
128 // This is a gross hack...
129 Layout const & old_layout = pars.begin()->layout();
132 pars.push_back(Paragraph());
133 pars.begin()->setInsetOwner(this);
134 pars.begin()->setLayout(old_layout);
138 Dimension const InsetText::dimension(BufferView const & bv) const
140 TextMetrics const & tm = bv.textMetrics(&text_);
141 Dimension dim = tm.dimension();
142 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
143 dim.des += TEXT_TO_INSET_OFFSET;
144 dim.asc += TEXT_TO_INSET_OFFSET;
149 void InsetText::write(ostream & os) const
152 text_.write(buffer(), os);
156 void InsetText::read(Lexer & lex)
160 // delete the initial paragraph
161 Paragraph oldpar = *paragraphs().begin();
162 paragraphs().clear();
164 lex.setContext("InsetText::read");
165 bool res = text_.read(buffer(), lex, errorList, this);
168 lex.printError("Missing \\end_inset at this point. ");
171 // ensure we have at least one paragraph.
172 if (paragraphs().empty())
173 paragraphs().push_back(oldpar);
177 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
179 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
181 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
183 // Hand font through to contained lyxtext:
184 tm.font_.fontInfo() = mi.base.font;
185 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
187 tm.metrics(mi, dim, mi.base.textwidth);
190 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
191 dim.asc += TEXT_TO_INSET_OFFSET;
192 dim.des += TEXT_TO_INSET_OFFSET;
193 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
197 void InsetText::draw(PainterInfo & pi, int x, int y) const
199 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
201 if (drawFrame_ || pi.full_repaint) {
202 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
203 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
204 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
205 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
207 pi.pain.fillRectangle(xframe, yframe, w, h,
208 pi.backgroundColor(this));
211 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
213 ColorCode const old_color = pi.background_color;
214 pi.background_color = pi.backgroundColor(this, false);
216 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
218 pi.background_color = old_color;
222 docstring InsetText::editMessage() const
224 return _("Opened Text Inset");
228 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
230 pit_type const pit = front ? 0 : paragraphs().size() - 1;
231 pos_type pos = front ? 0 : paragraphs().back().size();
233 // if visual information is not to be ignored, move to extreme right/left
234 if (entry_from != ENTRY_DIRECTION_IGNORE) {
235 Cursor temp_cur = cur;
236 temp_cur.pit() = pit;
237 temp_cur.pos() = pos;
238 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
239 pos = temp_cur.pos();
242 text_.setCursor(cur.top(), pit, pos);
243 cur.clearSelection();
248 Inset * InsetText::editXY(Cursor & cur, int x, int y)
250 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
254 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
256 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
257 << " [ cmd.action = " << cmd.action << ']');
259 // Dispatch only to text_ if the cursor is inside
260 // the text_. It is not for context menus (bug 5797).
261 if (cur.text() == &text_)
262 text_.dispatch(cur, cmd);
266 if (!cur.result().dispatched())
267 Inset::doDispatch(cur, cmd);
271 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
272 FuncStatus & status) const
274 switch (cmd.action) {
276 status.setEnabled(!forcePlainLayout());
279 case LFUN_LAYOUT_PARAGRAPH:
280 case LFUN_PARAGRAPH_PARAMS:
281 case LFUN_PARAGRAPH_PARAMS_APPLY:
282 case LFUN_PARAGRAPH_SPACING:
283 case LFUN_PARAGRAPH_UPDATE:
284 status.setEnabled(allowParagraphCustomization());
287 // Dispatch only to text_ if the cursor is inside
288 // the text_. It is not for context menus (bug 5797).
290 if (cur.text() == &text_)
291 ret = text_.getStatus(cur, cmd, status);
294 ret = Inset::getStatus(cur, cmd, status);
300 void InsetText::setChange(Change const & change)
302 ParagraphList::iterator pit = paragraphs().begin();
303 ParagraphList::iterator end = paragraphs().end();
304 for (; pit != end; ++pit) {
305 pit->setChange(change);
310 void InsetText::acceptChanges(BufferParams const & bparams)
312 text_.acceptChanges(bparams);
316 void InsetText::rejectChanges(BufferParams const & bparams)
318 text_.rejectChanges(bparams);
322 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
325 latexParagraphs(buffer(), text_, os, texrow, runparams);
326 return texrow.rows();
330 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
332 ParagraphList::const_iterator beg = paragraphs().begin();
333 ParagraphList::const_iterator end = paragraphs().end();
334 ParagraphList::const_iterator it = beg;
335 bool ref_printed = false;
337 for (; it != end; ++it) {
340 if (runparams.linelen > 0)
343 odocstringstream oss;
344 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
345 docstring const str = oss.str();
347 // FIXME: len is not computed fully correctly; in principle,
348 // we have to count the characters after the last '\n'
356 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
358 docbookParagraphs(paragraphs(), buffer(), os, runparams);
363 void InsetText::validate(LaTeXFeatures & features) const
365 for_each(paragraphs().begin(), paragraphs().end(),
366 bind(&Paragraph::validate, _1, ref(features)));
370 void InsetText::cursorPos(BufferView const & bv,
371 CursorSlice const & sl, bool boundary, int & x, int & y) const
373 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
374 y = bv.textMetrics(&text_).cursorY(sl, boundary);
378 bool InsetText::showInsetDialog(BufferView *) const
384 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
387 Paragraph & first = paragraphs().front();
388 for (unsigned int i = 0; i < data.length(); ++i)
389 first.insertChar(i, data[i], font, trackChanges);
393 void InsetText::setAutoBreakRows(bool flag)
395 if (flag == text_.autoBreakRows_)
398 text_.autoBreakRows_ = flag;
402 // remove previously existing newlines
403 ParagraphList::iterator it = paragraphs().begin();
404 ParagraphList::iterator end = paragraphs().end();
405 for (; it != end; ++it)
406 for (int i = 0; i < it->size(); ++i)
407 if (it->isNewline(i))
408 // do not track the change, because the user
409 // is not allowed to revert/reject it
410 it->eraseChar(i, false);
414 void InsetText::setDrawFrame(bool flag)
420 ColorCode InsetText::frameColor() const
426 void InsetText::setFrameColor(ColorCode col)
432 void InsetText::appendParagraphs(ParagraphList & plist)
434 // There is little we can do here to keep track of changes.
435 // As of 2006/10/20, appendParagraphs is used exclusively by
436 // LyXTabular::setMultiColumn. In this context, the paragraph break
437 // is lost irreversibly and the appended text doesn't really change
439 ParagraphList & pl = paragraphs();
441 ParagraphList::iterator pit = plist.begin();
442 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
444 mergeParagraph(buffer().params(), pl,
445 distance(pl.begin(), ins) - 1);
447 for_each(pit, plist.end(),
448 bind(&ParagraphList::push_back, ref(pl), _1));
452 void InsetText::addPreview(PreviewLoader & loader) const
454 ParagraphList::const_iterator pit = paragraphs().begin();
455 ParagraphList::const_iterator pend = paragraphs().end();
457 for (; pit != pend; ++pit) {
458 InsetList::const_iterator it = pit->insetList().begin();
459 InsetList::const_iterator end = pit->insetList().end();
460 for (; it != end; ++it)
461 it->inset->addPreview(loader);
466 ParagraphList const & InsetText::paragraphs() const
468 return text_.paragraphs();
472 ParagraphList & InsetText::paragraphs()
474 return text_.paragraphs();
478 void InsetText::updateLabels(ParIterator const & it)
480 ParIterator it2 = it;
482 LASSERT(&it2.inset() == this && it2.pit() == 0, return);
483 if (producesOutput()) {
484 buffer().updateLabels(it2);
486 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
487 Counters const savecnt = tclass.counters();
488 buffer().updateLabels(it2);
489 tclass.counters() = savecnt;
494 void InsetText::addToToc(DocIterator const & cdit)
496 DocIterator dit = cdit;
497 dit.push_back(CursorSlice(*this));
498 Toc & toc = buffer().tocBackend().toc("tableofcontents");
500 BufferParams const & bufparams = buffer_->params();
501 const int min_toclevel = bufparams.documentClass().min_toclevel();
503 // For each paragraph, traverse its insets and let them add
505 ParagraphList & pars = paragraphs();
506 pit_type pend = paragraphs().size();
507 for (pit_type pit = 0; pit != pend; ++pit) {
508 Paragraph const & par = pars[pit];
510 // the string that goes to the toc (could be the optarg)
512 InsetList::const_iterator it = par.insetList().begin();
513 InsetList::const_iterator end = par.insetList().end();
514 for (; it != end; ++it) {
515 Inset & inset = *it->inset;
517 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
519 switch (inset.lyxCode()) {
521 if (!tocstring.empty())
524 Paragraph const & insetpar =
525 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
526 if (!par.labelString().empty())
527 tocstring = par.labelString() + ' ';
528 tocstring += insetpar.asString(AS_STR_INSETS);
535 /// now the toc entry for the paragraph
536 int const toclevel = par.layout().toclevel;
537 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
539 // insert this into the table of contents
540 if (tocstring.empty())
541 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
542 toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
545 // And now the list of changes.
546 par.addChangesToToc(dit, buffer());
551 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
553 if (buffer().isClean())
554 return Inset::notifyCursorLeaves(old, cur);
556 // find text inset in old cursor
557 Cursor insetCur = old;
558 int scriptSlice = insetCur.find(this);
559 LASSERT(scriptSlice != -1, /**/);
560 insetCur.cutOff(scriptSlice);
561 LASSERT(&insetCur.inset() == this, /**/);
563 // update the old paragraph's words
564 insetCur.paragraph().updateWords();
566 return Inset::notifyCursorLeaves(old, cur);
570 bool InsetText::completionSupported(Cursor const & cur) const
572 //LASSERT(&cur.bv().cursor().inset() != this, return false);
573 return text_.completionSupported(cur);
577 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
579 return completionSupported(cur);
583 bool InsetText::automaticInlineCompletion() const
585 return lyxrc.completion_inline_text;
589 bool InsetText::automaticPopupCompletion() const
591 return lyxrc.completion_popup_text;
595 bool InsetText::showCompletionCursor() const
597 return lyxrc.completion_cursor_text;
601 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
603 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
607 docstring InsetText::completionPrefix(Cursor const & cur) const
609 if (!completionSupported(cur))
611 return text_.completionPrefix(cur);
615 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
618 if (!completionSupported(cur))
621 return text_.insertCompletion(cur, s, finished);
625 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
626 Dimension & dim) const
628 TextMetrics const & tm = cur.bv().textMetrics(&text_);
629 tm.completionPosAndDim(cur, x, y, dim);
633 docstring InsetText::contextMenu(BufferView const &, int, int) const
635 return from_ascii("context-edit");