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 // This can happen when a layout has a left and right margin,
188 // and the view is made very narrow. We can't do better than
189 // to draw it partly out of view (bug 5890).
190 if (mi.base.textwidth < 1)
191 mi.base.textwidth = 1;
194 tm.metrics(mi, dim, mi.base.textwidth);
197 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
198 dim.asc += TEXT_TO_INSET_OFFSET;
199 dim.des += TEXT_TO_INSET_OFFSET;
200 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
204 void InsetText::draw(PainterInfo & pi, int x, int y) const
206 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
208 if (drawFrame_ || pi.full_repaint) {
209 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
210 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
211 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
212 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
214 pi.pain.fillRectangle(xframe, yframe, w, h,
215 pi.backgroundColor(this));
218 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
220 ColorCode const old_color = pi.background_color;
221 pi.background_color = pi.backgroundColor(this, false);
223 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
225 pi.background_color = old_color;
229 docstring InsetText::editMessage() const
231 return _("Opened Text Inset");
235 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
237 pit_type const pit = front ? 0 : paragraphs().size() - 1;
238 pos_type pos = front ? 0 : paragraphs().back().size();
240 // if visual information is not to be ignored, move to extreme right/left
241 if (entry_from != ENTRY_DIRECTION_IGNORE) {
242 Cursor temp_cur = cur;
243 temp_cur.pit() = pit;
244 temp_cur.pos() = pos;
245 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
246 pos = temp_cur.pos();
249 text_.setCursor(cur.top(), pit, pos);
250 cur.clearSelection();
255 Inset * InsetText::editXY(Cursor & cur, int x, int y)
257 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
261 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
263 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
264 << " [ cmd.action = " << cmd.action << ']');
266 // Dispatch only to text_ if the cursor is inside
267 // the text_. It is not for context menus (bug 5797).
268 if (cur.text() == &text_)
269 text_.dispatch(cur, cmd);
273 if (!cur.result().dispatched())
274 Inset::doDispatch(cur, cmd);
278 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
279 FuncStatus & status) const
281 switch (cmd.action) {
283 status.setEnabled(!forcePlainLayout());
286 case LFUN_LAYOUT_PARAGRAPH:
287 case LFUN_PARAGRAPH_PARAMS:
288 case LFUN_PARAGRAPH_PARAMS_APPLY:
289 case LFUN_PARAGRAPH_SPACING:
290 case LFUN_PARAGRAPH_UPDATE:
291 status.setEnabled(allowParagraphCustomization());
294 // Dispatch only to text_ if the cursor is inside
295 // the text_. It is not for context menus (bug 5797).
297 if (cur.text() == &text_)
298 ret = text_.getStatus(cur, cmd, status);
301 ret = Inset::getStatus(cur, cmd, status);
307 void InsetText::setChange(Change const & change)
309 ParagraphList::iterator pit = paragraphs().begin();
310 ParagraphList::iterator end = paragraphs().end();
311 for (; pit != end; ++pit) {
312 pit->setChange(change);
317 void InsetText::acceptChanges(BufferParams const & bparams)
319 text_.acceptChanges(bparams);
323 void InsetText::rejectChanges(BufferParams const & bparams)
325 text_.rejectChanges(bparams);
329 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
332 latexParagraphs(buffer(), text_, os, texrow, runparams);
333 return texrow.rows();
337 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
339 ParagraphList::const_iterator beg = paragraphs().begin();
340 ParagraphList::const_iterator end = paragraphs().end();
341 ParagraphList::const_iterator it = beg;
342 bool ref_printed = false;
344 for (; it != end; ++it) {
347 if (runparams.linelen > 0)
350 odocstringstream oss;
351 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
352 docstring const str = oss.str();
354 // FIXME: len is not computed fully correctly; in principle,
355 // we have to count the characters after the last '\n'
363 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
365 docbookParagraphs(paragraphs(), buffer(), os, runparams);
370 void InsetText::validate(LaTeXFeatures & features) const
372 for_each(paragraphs().begin(), paragraphs().end(),
373 bind(&Paragraph::validate, _1, ref(features)));
377 void InsetText::cursorPos(BufferView const & bv,
378 CursorSlice const & sl, bool boundary, int & x, int & y) const
380 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
381 y = bv.textMetrics(&text_).cursorY(sl, boundary);
385 bool InsetText::showInsetDialog(BufferView *) const
391 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
394 Paragraph & first = paragraphs().front();
395 for (unsigned int i = 0; i < data.length(); ++i)
396 first.insertChar(i, data[i], font, trackChanges);
400 void InsetText::setAutoBreakRows(bool flag)
402 if (flag == text_.autoBreakRows_)
405 text_.autoBreakRows_ = flag;
409 // remove previously existing newlines
410 ParagraphList::iterator it = paragraphs().begin();
411 ParagraphList::iterator end = paragraphs().end();
412 for (; it != end; ++it)
413 for (int i = 0; i < it->size(); ++i)
414 if (it->isNewline(i))
415 // do not track the change, because the user
416 // is not allowed to revert/reject it
417 it->eraseChar(i, false);
421 void InsetText::setDrawFrame(bool flag)
427 ColorCode InsetText::frameColor() const
433 void InsetText::setFrameColor(ColorCode col)
439 void InsetText::appendParagraphs(ParagraphList & plist)
441 // There is little we can do here to keep track of changes.
442 // As of 2006/10/20, appendParagraphs is used exclusively by
443 // LyXTabular::setMultiColumn. In this context, the paragraph break
444 // is lost irreversibly and the appended text doesn't really change
446 ParagraphList & pl = paragraphs();
448 ParagraphList::iterator pit = plist.begin();
449 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
451 mergeParagraph(buffer().params(), pl,
452 distance(pl.begin(), ins) - 1);
454 for_each(pit, plist.end(),
455 bind(&ParagraphList::push_back, ref(pl), _1));
459 void InsetText::addPreview(PreviewLoader & loader) const
461 ParagraphList::const_iterator pit = paragraphs().begin();
462 ParagraphList::const_iterator pend = paragraphs().end();
464 for (; pit != pend; ++pit) {
465 InsetList::const_iterator it = pit->insetList().begin();
466 InsetList::const_iterator end = pit->insetList().end();
467 for (; it != end; ++it)
468 it->inset->addPreview(loader);
473 ParagraphList const & InsetText::paragraphs() const
475 return text_.paragraphs();
479 ParagraphList & InsetText::paragraphs()
481 return text_.paragraphs();
485 void InsetText::updateLabels(ParIterator const & it)
487 ParIterator it2 = it;
489 LASSERT(&it2.inset() == this && it2.pit() == 0, return);
490 if (producesOutput())
491 buffer().updateLabels(it2);
493 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
494 Counters const savecnt = tclass.counters();
495 buffer().updateLabels(it2);
496 tclass.counters() = savecnt;
501 void InsetText::addToToc(DocIterator const & cdit)
503 DocIterator dit = cdit;
504 dit.push_back(CursorSlice(*this));
505 Toc & toc = buffer().tocBackend().toc("tableofcontents");
507 BufferParams const & bufparams = buffer_->params();
508 const int min_toclevel = bufparams.documentClass().min_toclevel();
510 // For each paragraph, traverse its insets and let them add
512 ParagraphList & pars = paragraphs();
513 pit_type pend = paragraphs().size();
514 for (pit_type pit = 0; pit != pend; ++pit) {
515 Paragraph const & par = pars[pit];
517 // the string that goes to the toc (could be the optarg)
519 InsetList::const_iterator it = par.insetList().begin();
520 InsetList::const_iterator end = par.insetList().end();
521 for (; it != end; ++it) {
522 Inset & inset = *it->inset;
524 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
526 switch (inset.lyxCode()) {
528 if (!tocstring.empty())
531 Paragraph const & insetpar =
532 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
533 if (!par.labelString().empty())
534 tocstring = par.labelString() + ' ';
535 tocstring += insetpar.asString(AS_STR_INSETS);
542 /// now the toc entry for the paragraph
543 int const toclevel = par.layout().toclevel;
544 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
546 // insert this into the table of contents
547 if (tocstring.empty())
548 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
549 toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
552 // And now the list of changes.
553 par.addChangesToToc(dit, buffer());
558 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
560 if (buffer().isClean())
561 return Inset::notifyCursorLeaves(old, cur);
563 // find text inset in old cursor
564 Cursor insetCur = old;
565 int scriptSlice = insetCur.find(this);
566 LASSERT(scriptSlice != -1, /**/);
567 insetCur.cutOff(scriptSlice);
568 LASSERT(&insetCur.inset() == this, /**/);
570 // update the old paragraph's words
571 insetCur.paragraph().updateWords();
573 return Inset::notifyCursorLeaves(old, cur);
577 bool InsetText::completionSupported(Cursor const & cur) const
579 //LASSERT(&cur.bv().cursor().inset() != this, return false);
580 return text_.completionSupported(cur);
584 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
586 return completionSupported(cur);
590 bool InsetText::automaticInlineCompletion() const
592 return lyxrc.completion_inline_text;
596 bool InsetText::automaticPopupCompletion() const
598 return lyxrc.completion_popup_text;
602 bool InsetText::showCompletionCursor() const
604 return lyxrc.completion_cursor_text;
608 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
610 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
614 docstring InsetText::completionPrefix(Cursor const & cur) const
616 if (!completionSupported(cur))
618 return text_.completionPrefix(cur);
622 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
625 if (!completionSupported(cur))
628 return text_.insertCompletion(cur, s, finished);
632 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
633 Dimension & dim) const
635 TextMetrics const & tm = cur.bv().textMetrics(&text_);
636 tm.completionPosAndDim(cur, x, y, dim);
640 docstring InsetText::contextMenu(BufferView const &, int, int) const
642 return from_ascii("context-edit");