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 "buffer_funcs.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "CompletionList.h"
20 #include "CoordCache.h"
22 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
24 #include "ErrorList.h"
25 #include "FuncRequest.h"
26 #include "InsetList.h"
31 #include "MetricsInfo.h"
32 #include "output_docbook.h"
33 #include "output_latex.h"
34 #include "OutputParams.h"
35 #include "output_plaintext.h"
36 #include "paragraph_funcs.h"
37 #include "Paragraph.h"
38 #include "ParagraphParameters.h"
39 #include "ParIterator.h"
43 #include "TextClass.h"
45 #include "TextMetrics.h"
47 #include "frontends/alert.h"
48 #include "frontends/Painter.h"
50 #include "support/debug.h"
51 #include "support/gettext.h"
52 #include "support/lstrings.h"
54 #include <boost/bind.hpp>
55 #include <boost/assert.hpp>
58 using namespace lyx::support;
65 using graphics::PreviewLoader;
68 /////////////////////////////////////////////////////////////////////
70 InsetText::InsetText(Buffer const & buf)
71 : drawFrame_(false), frame_color_(Color_insetframe)
77 InsetText::InsetText(InsetText const & in)
80 text_.autoBreakRows_ = in.text_.autoBreakRows_;
81 drawFrame_ = in.drawFrame_;
82 frame_color_ = in.frame_color_;
83 text_.paragraphs() = in.text_.paragraphs();
88 InsetText::InsetText()
92 void InsetText::initParagraphs(Buffer const & buf)
94 BOOST_ASSERT(paragraphs().empty());
95 buffer_ = const_cast<Buffer *>(&buf);
96 paragraphs().push_back(Paragraph());
97 Paragraph & ourpar = paragraphs().back();
98 ourpar.setEmptyOrDefaultLayout(buf.params().documentClass());
99 ourpar.setInsetOwner(this);
102 void InsetText::setParagraphOwner()
104 for_each(paragraphs().begin(), paragraphs().end(),
105 bind(&Paragraph::setInsetOwner, _1, this));
109 void InsetText::clear()
111 ParagraphList & pars = paragraphs();
112 BOOST_ASSERT(!pars.empty());
114 // This is a gross hack...
115 Layout const & old_layout = pars.begin()->layout();
118 pars.push_back(Paragraph());
119 pars.begin()->setInsetOwner(this);
120 pars.begin()->setLayout(old_layout);
124 Dimension const InsetText::dimension(BufferView const & bv) const
126 TextMetrics const & tm = bv.textMetrics(&text_);
127 Dimension dim = tm.dimension();
128 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
129 dim.des += TEXT_TO_INSET_OFFSET;
130 dim.asc += TEXT_TO_INSET_OFFSET;
135 void InsetText::write(ostream & os) const
138 text_.write(buffer(), os);
142 void InsetText::read(Lexer & lex)
146 // delete the initial paragraph
147 Paragraph oldpar = *paragraphs().begin();
148 paragraphs().clear();
150 bool res = text_.read(buffer(), lex, errorList, this);
153 lex.printError("Missing \\end_inset at this point. "
158 // ensure we have at least one paragraph.
159 if (paragraphs().empty())
160 paragraphs().push_back(oldpar);
164 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
166 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
168 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
170 // Hand font through to contained lyxtext:
171 tm.font_.fontInfo() = mi.base.font;
172 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
174 tm.metrics(mi, dim, mi.base.textwidth);
177 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
178 dim.asc += TEXT_TO_INSET_OFFSET;
179 dim.des += TEXT_TO_INSET_OFFSET;
180 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
184 void InsetText::draw(PainterInfo & pi, int x, int y) const
186 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
188 if (drawFrame_ || pi.full_repaint) {
189 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
190 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
191 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
192 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
194 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
196 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
198 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
202 docstring InsetText::editMessage() const
204 return _("Opened Text Inset");
208 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
210 pit_type const pit = front ? 0 : paragraphs().size() - 1;
211 pos_type pos = front ? 0 : paragraphs().back().size();
213 // if visual information is not to be ignored, move to extreme right/left
214 if (entry_from != ENTRY_DIRECTION_IGNORE) {
215 Cursor temp_cur = cur;
216 temp_cur.pit() = pit;
217 temp_cur.pos() = pos;
218 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
219 pos = temp_cur.pos();
222 text_.setCursor(cur.top(), pit, pos);
223 cur.clearSelection();
228 Inset * InsetText::editXY(Cursor & cur, int x, int y)
230 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
234 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
236 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
237 << " [ cmd.action = " << cmd.action << ']');
238 text_.dispatch(cur, cmd);
242 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
243 FuncStatus & status) const
245 return text_.getStatus(cur, cmd, status);
249 void InsetText::setChange(Change const & change)
251 ParagraphList::iterator pit = paragraphs().begin();
252 ParagraphList::iterator end = paragraphs().end();
253 for (; pit != end; ++pit) {
254 pit->setChange(change);
259 void InsetText::acceptChanges(BufferParams const & bparams)
261 text_.acceptChanges(bparams);
265 void InsetText::rejectChanges(BufferParams const & bparams)
267 text_.rejectChanges(bparams);
271 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
274 latexParagraphs(buffer(), text_, os, texrow, runparams);
275 return texrow.rows();
279 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
281 ParagraphList::const_iterator beg = paragraphs().begin();
282 ParagraphList::const_iterator end = paragraphs().end();
283 ParagraphList::const_iterator it = beg;
284 bool ref_printed = false;
286 for (; it != end; ++it) {
289 if (runparams.linelen > 0)
292 odocstringstream oss;
293 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
294 docstring const str = oss.str();
296 // FIXME: len is not computed fully correctly; in principle,
297 // we have to count the characters after the last '\n'
305 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
307 docbookParagraphs(paragraphs(), buffer(), os, runparams);
312 void InsetText::validate(LaTeXFeatures & features) const
314 for_each(paragraphs().begin(), paragraphs().end(),
315 bind(&Paragraph::validate, _1, ref(features)));
319 void InsetText::cursorPos(BufferView const & bv,
320 CursorSlice const & sl, bool boundary, int & x, int & y) const
322 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
323 y = bv.textMetrics(&text_).cursorY(sl, boundary);
327 bool InsetText::showInsetDialog(BufferView *) const
333 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
336 Paragraph & first = paragraphs().front();
337 for (unsigned int i = 0; i < data.length(); ++i)
338 first.insertChar(i, data[i], font, trackChanges);
342 void InsetText::setAutoBreakRows(bool flag)
344 if (flag == text_.autoBreakRows_)
347 text_.autoBreakRows_ = flag;
351 // remove previously existing newlines
352 ParagraphList::iterator it = paragraphs().begin();
353 ParagraphList::iterator end = paragraphs().end();
354 for (; it != end; ++it)
355 for (int i = 0; i < it->size(); ++i)
356 if (it->isNewline(i))
357 // do not track the change, because the user
358 // is not allowed to revert/reject it
359 it->eraseChar(i, false);
363 void InsetText::setDrawFrame(bool flag)
369 ColorCode InsetText::frameColor() const
375 void InsetText::setFrameColor(ColorCode col)
381 void InsetText::appendParagraphs(ParagraphList & plist)
383 // There is little we can do here to keep track of changes.
384 // As of 2006/10/20, appendParagraphs is used exclusively by
385 // LyXTabular::setMultiColumn. In this context, the paragraph break
386 // is lost irreversibly and the appended text doesn't really change
388 ParagraphList & pl = paragraphs();
390 ParagraphList::iterator pit = plist.begin();
391 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
393 mergeParagraph(buffer().params(), pl,
394 distance(pl.begin(), ins) - 1);
396 for_each(pit, plist.end(),
397 bind(&ParagraphList::push_back, ref(pl), _1));
401 void InsetText::addPreview(PreviewLoader & loader) const
403 ParagraphList::const_iterator pit = paragraphs().begin();
404 ParagraphList::const_iterator pend = paragraphs().end();
406 for (; pit != pend; ++pit) {
407 InsetList::const_iterator it = pit->insetList().begin();
408 InsetList::const_iterator end = pit->insetList().end();
409 for (; it != end; ++it)
410 it->inset->addPreview(loader);
415 ParagraphList const & InsetText::paragraphs() const
417 return text_.paragraphs();
421 ParagraphList & InsetText::paragraphs()
423 return text_.paragraphs();
427 //void InsetInclude::addToToc(ParConstIterator const & cpit) const
432 void InsetText::updateLabels(ParIterator const & it)
434 ParIterator it2 = it;
436 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
437 lyx::updateLabels(buffer(), it2);
441 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
443 if (cur.buffer().isClean())
444 return Inset::notifyCursorLeaves(old, cur);
446 // find text inset in old cursor
447 Cursor insetCur = old;
448 int scriptSlice = insetCur.find(this);
449 BOOST_ASSERT(scriptSlice != -1);
450 insetCur.cutOff(scriptSlice);
451 BOOST_ASSERT(&insetCur.inset() == this);
453 // update the old paragraph's words
454 insetCur.paragraph().updateWords(insetCur.top());
456 return Inset::notifyCursorLeaves(old, cur);
460 bool InsetText::completionSupported(Cursor const & cur) const
462 Cursor const & bvCur = cur.bv().cursor();
463 if (&bvCur.inset() != this)
465 return text_.completionSupported(cur);
469 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
471 return completionSupported(cur);
475 bool InsetText::automaticInlineCompletion() const
477 return lyxrc.completion_inline_text;
481 bool InsetText::automaticPopupCompletion() const
483 return lyxrc.completion_popup_text;
487 bool InsetText::showCompletionCursor() const
489 return lyxrc.completion_cursor_text;
493 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
495 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
499 docstring InsetText::completionPrefix(Cursor const & cur) const
501 if (!completionSupported(cur))
503 return text_.completionPrefix(cur);
507 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
510 if (!completionSupported(cur))
513 return text_.insertCompletion(cur, s, finished);
517 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
518 Dimension & dim) const
520 TextMetrics const & tm = cur.bv().textMetrics(&text_);
521 tm.completionPosAndDim(cur, x, y, dim);
525 docstring InsetText::contextMenu(BufferView const &, int, int) const
527 return from_ascii("context-edit");