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);
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,
203 pi.backgroundColor(this));
206 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
208 ColorCode const old_color = pi.background_color;
209 pi.background_color = pi.backgroundColor(this, false);
211 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
213 pi.background_color = old_color;
217 docstring InsetText::editMessage() const
219 return _("Opened Text Inset");
223 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
225 pit_type const pit = front ? 0 : paragraphs().size() - 1;
226 pos_type pos = front ? 0 : paragraphs().back().size();
228 // if visual information is not to be ignored, move to extreme right/left
229 if (entry_from != ENTRY_DIRECTION_IGNORE) {
230 Cursor temp_cur = cur;
231 temp_cur.pit() = pit;
232 temp_cur.pos() = pos;
233 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
234 pos = temp_cur.pos();
237 text_.setCursor(cur.top(), pit, pos);
238 cur.clearSelection();
243 Inset * InsetText::editXY(Cursor & cur, int x, int y)
245 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
249 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
251 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
252 << " [ cmd.action = " << cmd.action << ']');
253 text_.dispatch(cur, cmd);
257 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
258 FuncStatus & status) const
260 switch (cmd.action) {
262 status.setEnabled(!forcePlainLayout());
265 case LFUN_LAYOUT_PARAGRAPH:
266 case LFUN_PARAGRAPH_PARAMS:
267 case LFUN_PARAGRAPH_PARAMS_APPLY:
268 case LFUN_PARAGRAPH_SPACING:
269 case LFUN_PARAGRAPH_UPDATE:
270 status.setEnabled(allowParagraphCustomization());
273 return text_.getStatus(cur, cmd, status);
278 void InsetText::setChange(Change const & change)
280 ParagraphList::iterator pit = paragraphs().begin();
281 ParagraphList::iterator end = paragraphs().end();
282 for (; pit != end; ++pit) {
283 pit->setChange(change);
288 void InsetText::acceptChanges(BufferParams const & bparams)
290 text_.acceptChanges(bparams);
294 void InsetText::rejectChanges(BufferParams const & bparams)
296 text_.rejectChanges(bparams);
300 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
303 latexParagraphs(buffer(), text_, os, texrow, runparams);
304 return texrow.rows();
308 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
310 ParagraphList::const_iterator beg = paragraphs().begin();
311 ParagraphList::const_iterator end = paragraphs().end();
312 ParagraphList::const_iterator it = beg;
313 bool ref_printed = false;
315 for (; it != end; ++it) {
318 if (runparams.linelen > 0)
321 odocstringstream oss;
322 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
323 docstring const str = oss.str();
325 // FIXME: len is not computed fully correctly; in principle,
326 // we have to count the characters after the last '\n'
334 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
336 docbookParagraphs(paragraphs(), buffer(), os, runparams);
341 void InsetText::validate(LaTeXFeatures & features) const
343 for_each(paragraphs().begin(), paragraphs().end(),
344 bind(&Paragraph::validate, _1, ref(features)));
348 void InsetText::cursorPos(BufferView const & bv,
349 CursorSlice const & sl, bool boundary, int & x, int & y) const
351 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
352 y = bv.textMetrics(&text_).cursorY(sl, boundary);
356 bool InsetText::showInsetDialog(BufferView *) const
362 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
365 Paragraph & first = paragraphs().front();
366 for (unsigned int i = 0; i < data.length(); ++i)
367 first.insertChar(i, data[i], font, trackChanges);
371 void InsetText::setAutoBreakRows(bool flag)
373 if (flag == text_.autoBreakRows_)
376 text_.autoBreakRows_ = flag;
380 // remove previously existing newlines
381 ParagraphList::iterator it = paragraphs().begin();
382 ParagraphList::iterator end = paragraphs().end();
383 for (; it != end; ++it)
384 for (int i = 0; i < it->size(); ++i)
385 if (it->isNewline(i))
386 // do not track the change, because the user
387 // is not allowed to revert/reject it
388 it->eraseChar(i, false);
392 void InsetText::setDrawFrame(bool flag)
398 ColorCode InsetText::frameColor() const
404 void InsetText::setFrameColor(ColorCode col)
410 void InsetText::appendParagraphs(ParagraphList & plist)
412 // There is little we can do here to keep track of changes.
413 // As of 2006/10/20, appendParagraphs is used exclusively by
414 // LyXTabular::setMultiColumn. In this context, the paragraph break
415 // is lost irreversibly and the appended text doesn't really change
417 ParagraphList & pl = paragraphs();
419 ParagraphList::iterator pit = plist.begin();
420 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
422 mergeParagraph(buffer().params(), pl,
423 distance(pl.begin(), ins) - 1);
425 for_each(pit, plist.end(),
426 bind(&ParagraphList::push_back, ref(pl), _1));
430 void InsetText::addPreview(PreviewLoader & loader) const
432 ParagraphList::const_iterator pit = paragraphs().begin();
433 ParagraphList::const_iterator pend = paragraphs().end();
435 for (; pit != pend; ++pit) {
436 InsetList::const_iterator it = pit->insetList().begin();
437 InsetList::const_iterator end = pit->insetList().end();
438 for (; it != end; ++it)
439 it->inset->addPreview(loader);
444 ParagraphList const & InsetText::paragraphs() const
446 return text_.paragraphs();
450 ParagraphList & InsetText::paragraphs()
452 return text_.paragraphs();
456 void InsetText::updateLabels(ParIterator const & it)
458 ParIterator it2 = it;
460 LASSERT(&it2.inset() == this && it2.pit() == 0, /**/);
461 if (producesOutput())
462 lyx::updateLabels(buffer(), it2);
464 DocumentClass const & tclass = buffer().params().documentClass();
465 Counters const savecnt = tclass.counters();
466 lyx::updateLabels(buffer(), it2);
467 tclass.counters() = savecnt;
472 void InsetText::addToToc(DocIterator const & cdit)
474 DocIterator dit = cdit;
475 dit.push_back(CursorSlice(*this));
476 Toc & toc = buffer().tocBackend().toc("tableofcontents");
478 BufferParams const & bufparams = buffer_->params();
479 const int min_toclevel = bufparams.documentClass().min_toclevel();
481 // For each paragraph, traverse its insets and let them add
483 ParagraphList & pars = paragraphs();
484 pit_type pend = paragraphs().size();
485 for (pit_type pit = 0; pit != pend; ++pit) {
486 Paragraph const & par = pars[pit];
488 // the string that goes to the toc (could be the optarg)
490 InsetList::const_iterator it = par.insetList().begin();
491 InsetList::const_iterator end = par.insetList().end();
492 for (; it != end; ++it) {
493 Inset & inset = *it->inset;
495 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
497 switch (inset.lyxCode()) {
499 if (!tocstring.empty())
502 Paragraph const & insetpar =
503 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
504 if (!par.labelString().empty())
505 tocstring = par.labelString() + ' ';
506 tocstring += insetpar.asString();
513 /// now the toc entry for the paragraph
514 int const toclevel = par.layout().toclevel;
515 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
517 // insert this into the table of contents
518 if (tocstring.empty())
519 tocstring = par.asString(AS_STR_LABEL);
520 toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
523 // And now the list of changes.
524 par.addChangesToToc(dit, buffer());
529 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
531 if (cur.buffer().isClean())
532 return Inset::notifyCursorLeaves(old, cur);
534 // find text inset in old cursor
535 Cursor insetCur = old;
536 int scriptSlice = insetCur.find(this);
537 LASSERT(scriptSlice != -1, /**/);
538 insetCur.cutOff(scriptSlice);
539 LASSERT(&insetCur.inset() == this, /**/);
541 // update the old paragraph's words
542 insetCur.paragraph().updateWords(insetCur.top());
544 return Inset::notifyCursorLeaves(old, cur);
548 bool InsetText::completionSupported(Cursor const & cur) const
550 Cursor const & bvCur = cur.bv().cursor();
551 if (&bvCur.inset() != this)
553 return text_.completionSupported(cur);
557 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
559 return completionSupported(cur);
563 bool InsetText::automaticInlineCompletion() const
565 return lyxrc.completion_inline_text;
569 bool InsetText::automaticPopupCompletion() const
571 return lyxrc.completion_popup_text;
575 bool InsetText::showCompletionCursor() const
577 return lyxrc.completion_cursor_text;
581 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
583 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
587 docstring InsetText::completionPrefix(Cursor const & cur) const
589 if (!completionSupported(cur))
591 return text_.completionPrefix(cur);
595 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
598 if (!completionSupported(cur))
601 return text_.insertCompletion(cur, s, finished);
605 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
606 Dimension & dim) const
608 TextMetrics const & tm = cur.bv().textMetrics(&text_);
609 tm.completionPosAndDim(cur, x, y, dim);
613 docstring InsetText::contextMenu(BufferView const &, int, int) const
615 return from_ascii("context-edit");