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 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()
104 LASSERT(paragraphs().empty(), /**/);
105 paragraphs().push_back(Paragraph());
106 Paragraph & ourpar = paragraphs().back();
107 ourpar.setInsetOwner(this);
108 ourpar.setDefaultLayout(buffer_->params().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,
204 pi.backgroundColor(this));
207 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
209 ColorCode const old_color = pi.background_color;
210 pi.background_color = pi.backgroundColor(this, false);
212 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
214 pi.background_color = old_color;
218 docstring InsetText::editMessage() const
220 return _("Opened Text Inset");
224 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
226 pit_type const pit = front ? 0 : paragraphs().size() - 1;
227 pos_type pos = front ? 0 : paragraphs().back().size();
229 // if visual information is not to be ignored, move to extreme right/left
230 if (entry_from != ENTRY_DIRECTION_IGNORE) {
231 Cursor temp_cur = cur;
232 temp_cur.pit() = pit;
233 temp_cur.pos() = pos;
234 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
235 pos = temp_cur.pos();
238 text_.setCursor(cur.top(), pit, pos);
239 cur.clearSelection();
244 Inset * InsetText::editXY(Cursor & cur, int x, int y)
246 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
250 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
252 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
253 << " [ cmd.action = " << cmd.action << ']');
254 text_.dispatch(cur, cmd);
258 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
259 FuncStatus & status) const
261 switch (cmd.action) {
263 status.setEnabled(!forcePlainLayout());
266 case LFUN_LAYOUT_PARAGRAPH:
267 case LFUN_PARAGRAPH_PARAMS:
268 case LFUN_PARAGRAPH_PARAMS_APPLY:
269 case LFUN_PARAGRAPH_SPACING:
270 case LFUN_PARAGRAPH_UPDATE:
271 status.setEnabled(allowParagraphCustomization());
274 // Dispatch only to text_ if the cursor is inside
275 // the text_. It is not for context menus (bug 5797).
276 if (cur.text() == &text_)
277 return text_.getStatus(cur, cmd, status);
283 void InsetText::setChange(Change const & change)
285 ParagraphList::iterator pit = paragraphs().begin();
286 ParagraphList::iterator end = paragraphs().end();
287 for (; pit != end; ++pit) {
288 pit->setChange(change);
293 void InsetText::acceptChanges(BufferParams const & bparams)
295 text_.acceptChanges(bparams);
299 void InsetText::rejectChanges(BufferParams const & bparams)
301 text_.rejectChanges(bparams);
305 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
308 latexParagraphs(buffer(), text_, os, texrow, runparams);
309 return texrow.rows();
313 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
315 ParagraphList::const_iterator beg = paragraphs().begin();
316 ParagraphList::const_iterator end = paragraphs().end();
317 ParagraphList::const_iterator it = beg;
318 bool ref_printed = false;
320 for (; it != end; ++it) {
323 if (runparams.linelen > 0)
326 odocstringstream oss;
327 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
328 docstring const str = oss.str();
330 // FIXME: len is not computed fully correctly; in principle,
331 // we have to count the characters after the last '\n'
339 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
341 docbookParagraphs(paragraphs(), buffer(), os, runparams);
346 void InsetText::validate(LaTeXFeatures & features) const
348 for_each(paragraphs().begin(), paragraphs().end(),
349 bind(&Paragraph::validate, _1, ref(features)));
353 void InsetText::cursorPos(BufferView const & bv,
354 CursorSlice const & sl, bool boundary, int & x, int & y) const
356 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
357 y = bv.textMetrics(&text_).cursorY(sl, boundary);
361 bool InsetText::showInsetDialog(BufferView *) const
367 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
370 Paragraph & first = paragraphs().front();
371 for (unsigned int i = 0; i < data.length(); ++i)
372 first.insertChar(i, data[i], font, trackChanges);
376 void InsetText::setAutoBreakRows(bool flag)
378 if (flag == text_.autoBreakRows_)
381 text_.autoBreakRows_ = flag;
385 // remove previously existing newlines
386 ParagraphList::iterator it = paragraphs().begin();
387 ParagraphList::iterator end = paragraphs().end();
388 for (; it != end; ++it)
389 for (int i = 0; i < it->size(); ++i)
390 if (it->isNewline(i))
391 // do not track the change, because the user
392 // is not allowed to revert/reject it
393 it->eraseChar(i, false);
397 void InsetText::setDrawFrame(bool flag)
403 ColorCode InsetText::frameColor() const
409 void InsetText::setFrameColor(ColorCode col)
415 void InsetText::appendParagraphs(ParagraphList & plist)
417 // There is little we can do here to keep track of changes.
418 // As of 2006/10/20, appendParagraphs is used exclusively by
419 // LyXTabular::setMultiColumn. In this context, the paragraph break
420 // is lost irreversibly and the appended text doesn't really change
422 ParagraphList & pl = paragraphs();
424 ParagraphList::iterator pit = plist.begin();
425 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
427 mergeParagraph(buffer().params(), pl,
428 distance(pl.begin(), ins) - 1);
430 for_each(pit, plist.end(),
431 bind(&ParagraphList::push_back, ref(pl), _1));
435 void InsetText::addPreview(PreviewLoader & loader) const
437 ParagraphList::const_iterator pit = paragraphs().begin();
438 ParagraphList::const_iterator pend = paragraphs().end();
440 for (; pit != pend; ++pit) {
441 InsetList::const_iterator it = pit->insetList().begin();
442 InsetList::const_iterator end = pit->insetList().end();
443 for (; it != end; ++it)
444 it->inset->addPreview(loader);
449 ParagraphList const & InsetText::paragraphs() const
451 return text_.paragraphs();
455 ParagraphList & InsetText::paragraphs()
457 return text_.paragraphs();
461 void InsetText::updateLabels(ParIterator const & it)
463 ParIterator it2 = it;
465 LASSERT(&it2.inset() == this && it2.pit() == 0, return);
466 if (producesOutput()) {
467 buffer().updateLabels(it2);
469 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
470 Counters const savecnt = tclass.counters();
471 buffer().updateLabels(it2);
472 tclass.counters() = savecnt;
477 void InsetText::addToToc(DocIterator const & cdit)
479 DocIterator dit = cdit;
480 dit.push_back(CursorSlice(*this));
481 Toc & toc = buffer().tocBackend().toc("tableofcontents");
483 BufferParams const & bufparams = buffer_->params();
484 const int min_toclevel = bufparams.documentClass().min_toclevel();
486 // For each paragraph, traverse its insets and let them add
488 ParagraphList & pars = paragraphs();
489 pit_type pend = paragraphs().size();
490 for (pit_type pit = 0; pit != pend; ++pit) {
491 Paragraph const & par = pars[pit];
493 // the string that goes to the toc (could be the optarg)
495 InsetList::const_iterator it = par.insetList().begin();
496 InsetList::const_iterator end = par.insetList().end();
497 for (; it != end; ++it) {
498 Inset & inset = *it->inset;
500 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
502 switch (inset.lyxCode()) {
504 if (!tocstring.empty())
507 Paragraph const & insetpar =
508 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
509 if (!par.labelString().empty())
510 tocstring = par.labelString() + ' ';
511 tocstring += insetpar.asString(AS_STR_INSETS);
518 /// now the toc entry for the paragraph
519 int const toclevel = par.layout().toclevel;
520 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
522 // insert this into the table of contents
523 if (tocstring.empty())
524 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
525 toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
528 // And now the list of changes.
529 par.addChangesToToc(dit, buffer());
534 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
536 if (buffer().isClean())
537 return Inset::notifyCursorLeaves(old, cur);
539 // find text inset in old cursor
540 Cursor insetCur = old;
541 int scriptSlice = insetCur.find(this);
542 LASSERT(scriptSlice != -1, /**/);
543 insetCur.cutOff(scriptSlice);
544 LASSERT(&insetCur.inset() == this, /**/);
546 // update the old paragraph's words
547 insetCur.paragraph().updateWords(insetCur.top());
549 return Inset::notifyCursorLeaves(old, cur);
553 bool InsetText::completionSupported(Cursor const & cur) const
555 //LASSERT(&cur.bv().cursor().inset() != this, return false);
556 return text_.completionSupported(cur);
560 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
562 return completionSupported(cur);
566 bool InsetText::automaticInlineCompletion() const
568 return lyxrc.completion_inline_text;
572 bool InsetText::automaticPopupCompletion() const
574 return lyxrc.completion_popup_text;
578 bool InsetText::showCompletionCursor() const
580 return lyxrc.completion_cursor_text;
584 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
586 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
590 docstring InsetText::completionPrefix(Cursor const & cur) const
592 if (!completionSupported(cur))
594 return text_.completionPrefix(cur);
598 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
601 if (!completionSupported(cur))
604 return text_.insertCompletion(cur, s, finished);
608 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
609 Dimension & dim) const
611 TextMetrics const & tm = cur.bv().textMetrics(&text_);
612 tm.completionPosAndDim(cur, x, y, dim);
616 docstring InsetText::contextMenu(BufferView const &, int, int) const
618 return from_ascii("context-edit");