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 "output_xhtml.h"
38 #include "OutputParams.h"
39 #include "output_plaintext.h"
40 #include "paragraph_funcs.h"
41 #include "Paragraph.h"
42 #include "ParagraphParameters.h"
43 #include "ParIterator.h"
47 #include "TextClass.h"
49 #include "TextMetrics.h"
50 #include "TocBackend.h"
52 #include "frontends/alert.h"
53 #include "frontends/Painter.h"
55 #include "support/debug.h"
56 #include "support/gettext.h"
57 #include "support/lstrings.h"
59 #include <boost/bind.hpp>
60 #include "support/lassert.h"
63 using namespace lyx::support;
70 using graphics::PreviewLoader;
73 /////////////////////////////////////////////////////////////////////
75 InsetText::InsetText(Buffer const & buf, UsePlain type)
76 : drawFrame_(false), frame_color_(Color_insetframe)
78 setBuffer(const_cast<Buffer &>(buf));
83 InsetText::InsetText(InsetText const & in)
86 text_.autoBreakRows_ = in.text_.autoBreakRows_;
87 drawFrame_ = in.drawFrame_;
88 frame_color_ = in.frame_color_;
89 text_.paragraphs() = in.text_.paragraphs();
94 void InsetText::setBuffer(Buffer & buf)
96 ParagraphList::iterator end = paragraphs().end();
97 for (ParagraphList::iterator it = paragraphs().begin(); it != end; ++it)
99 Inset::setBuffer(buf);
103 void InsetText::initParagraphs(UsePlain type)
105 LASSERT(paragraphs().empty(), /**/);
106 paragraphs().push_back(Paragraph());
107 Paragraph & ourpar = paragraphs().back();
108 ourpar.setInsetOwner(this);
109 DocumentClass const & dc = buffer_->params().documentClass();
110 if (type == DefaultLayout)
111 ourpar.setDefaultLayout(dc);
113 ourpar.setPlainLayout(dc);
117 void InsetText::setParagraphOwner()
119 for_each(paragraphs().begin(), paragraphs().end(),
120 bind(&Paragraph::setInsetOwner, _1, this));
124 void InsetText::clear()
126 ParagraphList & pars = paragraphs();
127 LASSERT(!pars.empty(), /**/);
129 // This is a gross hack...
130 Layout const & old_layout = pars.begin()->layout();
133 pars.push_back(Paragraph());
134 pars.begin()->setInsetOwner(this);
135 pars.begin()->setLayout(old_layout);
139 Dimension const InsetText::dimension(BufferView const & bv) const
141 TextMetrics const & tm = bv.textMetrics(&text_);
142 Dimension dim = tm.dimension();
143 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
144 dim.des += TEXT_TO_INSET_OFFSET;
145 dim.asc += TEXT_TO_INSET_OFFSET;
150 void InsetText::write(ostream & os) const
153 text_.write(buffer(), os);
157 void InsetText::read(Lexer & lex)
161 // delete the initial paragraph
162 Paragraph oldpar = *paragraphs().begin();
163 paragraphs().clear();
165 lex.setContext("InsetText::read");
166 bool res = text_.read(buffer(), lex, errorList, this);
169 lex.printError("Missing \\end_inset at this point. ");
172 // ensure we have at least one paragraph.
173 if (paragraphs().empty())
174 paragraphs().push_back(oldpar);
178 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
180 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
182 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
184 // Hand font through to contained lyxtext:
185 tm.font_.fontInfo() = mi.base.font;
186 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
188 // This can happen when a layout has a left and right margin,
189 // and the view is made very narrow. We can't do better than
190 // to draw it partly out of view (bug 5890).
191 if (mi.base.textwidth < 1)
192 mi.base.textwidth = 1;
195 tm.metrics(mi, dim, mi.base.textwidth);
198 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
199 dim.asc += TEXT_TO_INSET_OFFSET;
200 dim.des += TEXT_TO_INSET_OFFSET;
201 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
205 void InsetText::draw(PainterInfo & pi, int x, int y) const
207 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
209 if (drawFrame_ || pi.full_repaint) {
210 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
211 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
212 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
213 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
215 pi.pain.fillRectangle(xframe, yframe, w, h,
216 pi.backgroundColor(this));
219 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
221 ColorCode const old_color = pi.background_color;
222 pi.background_color = pi.backgroundColor(this, false);
224 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
226 pi.background_color = old_color;
230 docstring InsetText::editMessage() const
232 return _("Opened Text Inset");
236 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
238 pit_type const pit = front ? 0 : paragraphs().size() - 1;
239 pos_type pos = front ? 0 : paragraphs().back().size();
241 // if visual information is not to be ignored, move to extreme right/left
242 if (entry_from != ENTRY_DIRECTION_IGNORE) {
243 Cursor temp_cur = cur;
244 temp_cur.pit() = pit;
245 temp_cur.pos() = pos;
246 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
247 pos = temp_cur.pos();
250 text_.setCursor(cur.top(), pit, pos);
251 cur.clearSelection();
256 Inset * InsetText::editXY(Cursor & cur, int x, int y)
258 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
262 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
264 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
265 << " [ cmd.action = " << cmd.action << ']');
267 // Dispatch only to text_ if the cursor is inside
268 // the text_. It is not for context menus (bug 5797).
269 if (cur.text() == &text_)
270 text_.dispatch(cur, cmd);
274 if (!cur.result().dispatched())
275 Inset::doDispatch(cur, cmd);
279 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
280 FuncStatus & status) const
282 switch (cmd.action) {
284 status.setEnabled(!forcePlainLayout());
287 case LFUN_LAYOUT_PARAGRAPH:
288 case LFUN_PARAGRAPH_PARAMS:
289 case LFUN_PARAGRAPH_PARAMS_APPLY:
290 case LFUN_PARAGRAPH_SPACING:
291 case LFUN_PARAGRAPH_UPDATE:
292 status.setEnabled(allowParagraphCustomization());
295 // Dispatch only to text_ if the cursor is inside
296 // the text_. It is not for context menus (bug 5797).
298 if (cur.text() == &text_)
299 ret = text_.getStatus(cur, cmd, status);
302 ret = Inset::getStatus(cur, cmd, status);
308 void InsetText::setChange(Change const & change)
310 ParagraphList::iterator pit = paragraphs().begin();
311 ParagraphList::iterator end = paragraphs().end();
312 for (; pit != end; ++pit) {
313 pit->setChange(change);
318 void InsetText::acceptChanges(BufferParams const & bparams)
320 text_.acceptChanges(bparams);
324 void InsetText::rejectChanges(BufferParams const & bparams)
326 text_.rejectChanges(bparams);
330 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
333 latexParagraphs(buffer(), text_, os, texrow, runparams);
334 return texrow.rows();
338 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
340 ParagraphList::const_iterator beg = paragraphs().begin();
341 ParagraphList::const_iterator end = paragraphs().end();
342 ParagraphList::const_iterator it = beg;
343 bool ref_printed = false;
345 for (; it != end; ++it) {
348 if (runparams.linelen > 0)
351 odocstringstream oss;
352 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
353 docstring const str = oss.str();
355 // FIXME: len is not computed fully correctly; in principle,
356 // we have to count the characters after the last '\n'
364 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
366 docbookParagraphs(paragraphs(), buffer(), os, runparams);
371 int InsetText::xhtml(odocstream & os, OutputParams const & runparams) const
373 xhtmlParagraphs(paragraphs(), buffer(), os, runparams);
378 void InsetText::validate(LaTeXFeatures & features) const
380 for_each(paragraphs().begin(), paragraphs().end(),
381 bind(&Paragraph::validate, _1, ref(features)));
385 void InsetText::cursorPos(BufferView const & bv,
386 CursorSlice const & sl, bool boundary, int & x, int & y) const
388 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
389 y = bv.textMetrics(&text_).cursorY(sl, boundary);
393 bool InsetText::showInsetDialog(BufferView *) const
399 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
402 Paragraph & first = paragraphs().front();
403 for (unsigned int i = 0; i < data.length(); ++i)
404 first.insertChar(i, data[i], font, trackChanges);
408 void InsetText::setAutoBreakRows(bool flag)
410 if (flag == text_.autoBreakRows_)
413 text_.autoBreakRows_ = flag;
417 // remove previously existing newlines
418 ParagraphList::iterator it = paragraphs().begin();
419 ParagraphList::iterator end = paragraphs().end();
420 for (; it != end; ++it)
421 for (int i = 0; i < it->size(); ++i)
422 if (it->isNewline(i))
423 // do not track the change, because the user
424 // is not allowed to revert/reject it
425 it->eraseChar(i, false);
429 void InsetText::setDrawFrame(bool flag)
435 ColorCode InsetText::frameColor() const
441 void InsetText::setFrameColor(ColorCode col)
447 void InsetText::appendParagraphs(ParagraphList & plist)
449 // There is little we can do here to keep track of changes.
450 // As of 2006/10/20, appendParagraphs is used exclusively by
451 // LyXTabular::setMultiColumn. In this context, the paragraph break
452 // is lost irreversibly and the appended text doesn't really change
454 ParagraphList & pl = paragraphs();
456 ParagraphList::iterator pit = plist.begin();
457 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
459 mergeParagraph(buffer().params(), pl,
460 distance(pl.begin(), ins) - 1);
462 for_each(pit, plist.end(),
463 bind(&ParagraphList::push_back, ref(pl), _1));
467 void InsetText::addPreview(PreviewLoader & loader) const
469 ParagraphList::const_iterator pit = paragraphs().begin();
470 ParagraphList::const_iterator pend = paragraphs().end();
472 for (; pit != pend; ++pit) {
473 InsetList::const_iterator it = pit->insetList().begin();
474 InsetList::const_iterator end = pit->insetList().end();
475 for (; it != end; ++it)
476 it->inset->addPreview(loader);
481 ParagraphList const & InsetText::paragraphs() const
483 return text_.paragraphs();
487 ParagraphList & InsetText::paragraphs()
489 return text_.paragraphs();
493 void InsetText::updateLabels(ParIterator const & it)
495 ParIterator it2 = it;
497 LASSERT(&it2.inset() == this && it2.pit() == 0, return);
498 if (producesOutput())
499 buffer().updateLabels(it2);
501 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
502 Counters const savecnt = tclass.counters();
503 buffer().updateLabels(it2);
504 tclass.counters() = savecnt;
509 void InsetText::addToToc(DocIterator const & cdit)
511 DocIterator dit = cdit;
512 dit.push_back(CursorSlice(*this));
513 Toc & toc = buffer().tocBackend().toc("tableofcontents");
515 BufferParams const & bufparams = buffer_->params();
516 const int min_toclevel = bufparams.documentClass().min_toclevel();
518 // For each paragraph, traverse its insets and let them add
520 ParagraphList & pars = paragraphs();
521 pit_type pend = paragraphs().size();
522 for (pit_type pit = 0; pit != pend; ++pit) {
523 Paragraph const & par = pars[pit];
525 // the string that goes to the toc (could be the optarg)
527 InsetList::const_iterator it = par.insetList().begin();
528 InsetList::const_iterator end = par.insetList().end();
529 for (; it != end; ++it) {
530 Inset & inset = *it->inset;
532 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
534 switch (inset.lyxCode()) {
536 if (!tocstring.empty())
539 Paragraph const & insetpar =
540 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
541 if (!par.labelString().empty())
542 tocstring = par.labelString() + ' ';
543 tocstring += insetpar.asString(AS_STR_INSETS);
550 /// now the toc entry for the paragraph
551 int const toclevel = par.layout().toclevel;
552 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
554 // insert this into the table of contents
555 if (tocstring.empty())
556 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
557 toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
560 // And now the list of changes.
561 par.addChangesToToc(dit, buffer());
566 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
568 if (buffer().isClean())
569 return Inset::notifyCursorLeaves(old, cur);
571 // find text inset in old cursor
572 Cursor insetCur = old;
573 int scriptSlice = insetCur.find(this);
574 LASSERT(scriptSlice != -1, /**/);
575 insetCur.cutOff(scriptSlice);
576 LASSERT(&insetCur.inset() == this, /**/);
578 // update the old paragraph's words
579 insetCur.paragraph().updateWords();
581 return Inset::notifyCursorLeaves(old, cur);
585 bool InsetText::completionSupported(Cursor const & cur) const
587 //LASSERT(&cur.bv().cursor().inset() != this, return false);
588 return text_.completionSupported(cur);
592 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
594 return completionSupported(cur);
598 bool InsetText::automaticInlineCompletion() const
600 return lyxrc.completion_inline_text;
604 bool InsetText::automaticPopupCompletion() const
606 return lyxrc.completion_popup_text;
610 bool InsetText::showCompletionCursor() const
612 return lyxrc.completion_cursor_text;
616 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
618 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
622 docstring InsetText::completionPrefix(Cursor const & cur) const
624 if (!completionSupported(cur))
626 return text_.completionPrefix(cur);
630 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
633 if (!completionSupported(cur))
636 return text_.insertCompletion(cur, s, finished);
640 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
641 Dimension & dim) const
643 TextMetrics const & tm = cur.bv().textMetrics(&text_);
644 tm.completionPosAndDim(cur, x, y, dim);
648 docstring InsetText::contextMenu(BufferView const &, int, int) const
650 return from_ascii("context-edit");