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)
73 Inset::setBuffer(const_cast<Buffer &>(buf));
78 InsetText::InsetText(InsetText const & in)
81 text_.autoBreakRows_ = in.text_.autoBreakRows_;
82 drawFrame_ = in.drawFrame_;
83 frame_color_ = in.frame_color_;
84 text_.paragraphs() = in.text_.paragraphs();
89 void InsetText::initParagraphs(Buffer const & buf)
91 BOOST_ASSERT(paragraphs().empty());
92 buffer_ = const_cast<Buffer *>(&buf);
93 paragraphs().push_back(Paragraph());
94 Paragraph & ourpar = paragraphs().back();
95 ourpar.setEmptyOrDefaultLayout(buf.params().documentClass());
96 ourpar.setInsetOwner(this);
99 void InsetText::setParagraphOwner()
101 for_each(paragraphs().begin(), paragraphs().end(),
102 bind(&Paragraph::setInsetOwner, _1, this));
106 void InsetText::clear()
108 ParagraphList & pars = paragraphs();
109 BOOST_ASSERT(!pars.empty());
111 // This is a gross hack...
112 Layout const & old_layout = pars.begin()->layout();
115 pars.push_back(Paragraph());
116 pars.begin()->setInsetOwner(this);
117 pars.begin()->setLayout(old_layout);
121 Dimension const InsetText::dimension(BufferView const & bv) const
123 TextMetrics const & tm = bv.textMetrics(&text_);
124 Dimension dim = tm.dimension();
125 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
126 dim.des += TEXT_TO_INSET_OFFSET;
127 dim.asc += TEXT_TO_INSET_OFFSET;
132 void InsetText::write(ostream & os) const
135 text_.write(buffer(), os);
139 void InsetText::read(Lexer & lex)
143 // delete the initial paragraph
144 Paragraph oldpar = *paragraphs().begin();
145 paragraphs().clear();
147 bool res = text_.read(buffer(), lex, errorList, this);
150 lex.printError("Missing \\end_inset at this point. "
155 // ensure we have at least one paragraph.
156 if (paragraphs().empty())
157 paragraphs().push_back(oldpar);
161 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
163 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
165 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
167 // Hand font through to contained lyxtext:
168 tm.font_.fontInfo() = mi.base.font;
169 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
171 tm.metrics(mi, dim, mi.base.textwidth);
174 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
175 dim.asc += TEXT_TO_INSET_OFFSET;
176 dim.des += TEXT_TO_INSET_OFFSET;
177 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
181 void InsetText::draw(PainterInfo & pi, int x, int y) const
183 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
185 if (drawFrame_ || pi.full_repaint) {
186 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
187 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
188 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
189 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
191 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
193 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
195 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
199 docstring InsetText::editMessage() const
201 return _("Opened Text Inset");
205 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
207 pit_type const pit = front ? 0 : paragraphs().size() - 1;
208 pos_type pos = front ? 0 : paragraphs().back().size();
210 // if visual information is not to be ignored, move to extreme right/left
211 if (entry_from != ENTRY_DIRECTION_IGNORE) {
212 Cursor temp_cur = cur;
213 temp_cur.pit() = pit;
214 temp_cur.pos() = pos;
215 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
216 pos = temp_cur.pos();
219 text_.setCursor(cur.top(), pit, pos);
220 cur.clearSelection();
225 Inset * InsetText::editXY(Cursor & cur, int x, int y)
227 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
231 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
233 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
234 << " [ cmd.action = " << cmd.action << ']');
235 text_.dispatch(cur, cmd);
239 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
240 FuncStatus & status) const
242 return text_.getStatus(cur, cmd, status);
246 void InsetText::setChange(Change const & change)
248 ParagraphList::iterator pit = paragraphs().begin();
249 ParagraphList::iterator end = paragraphs().end();
250 for (; pit != end; ++pit) {
251 pit->setChange(change);
256 void InsetText::acceptChanges(BufferParams const & bparams)
258 text_.acceptChanges(bparams);
262 void InsetText::rejectChanges(BufferParams const & bparams)
264 text_.rejectChanges(bparams);
268 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
271 latexParagraphs(buffer(), text_, os, texrow, runparams);
272 return texrow.rows();
276 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
278 ParagraphList::const_iterator beg = paragraphs().begin();
279 ParagraphList::const_iterator end = paragraphs().end();
280 ParagraphList::const_iterator it = beg;
281 bool ref_printed = false;
283 for (; it != end; ++it) {
286 if (runparams.linelen > 0)
289 odocstringstream oss;
290 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
291 docstring const str = oss.str();
293 // FIXME: len is not computed fully correctly; in principle,
294 // we have to count the characters after the last '\n'
302 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
304 docbookParagraphs(paragraphs(), buffer(), os, runparams);
309 void InsetText::validate(LaTeXFeatures & features) const
311 for_each(paragraphs().begin(), paragraphs().end(),
312 bind(&Paragraph::validate, _1, ref(features)));
316 void InsetText::cursorPos(BufferView const & bv,
317 CursorSlice const & sl, bool boundary, int & x, int & y) const
319 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
320 y = bv.textMetrics(&text_).cursorY(sl, boundary);
324 bool InsetText::showInsetDialog(BufferView *) const
330 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
333 Paragraph & first = paragraphs().front();
334 for (unsigned int i = 0; i < data.length(); ++i)
335 first.insertChar(i, data[i], font, trackChanges);
339 void InsetText::setAutoBreakRows(bool flag)
341 if (flag == text_.autoBreakRows_)
344 text_.autoBreakRows_ = flag;
348 // remove previously existing newlines
349 ParagraphList::iterator it = paragraphs().begin();
350 ParagraphList::iterator end = paragraphs().end();
351 for (; it != end; ++it)
352 for (int i = 0; i < it->size(); ++i)
353 if (it->isNewline(i))
354 // do not track the change, because the user
355 // is not allowed to revert/reject it
356 it->eraseChar(i, false);
360 void InsetText::setDrawFrame(bool flag)
366 ColorCode InsetText::frameColor() const
372 void InsetText::setFrameColor(ColorCode col)
378 void InsetText::appendParagraphs(ParagraphList & plist)
380 // There is little we can do here to keep track of changes.
381 // As of 2006/10/20, appendParagraphs is used exclusively by
382 // LyXTabular::setMultiColumn. In this context, the paragraph break
383 // is lost irreversibly and the appended text doesn't really change
385 ParagraphList & pl = paragraphs();
387 ParagraphList::iterator pit = plist.begin();
388 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
390 mergeParagraph(buffer().params(), pl,
391 distance(pl.begin(), ins) - 1);
393 for_each(pit, plist.end(),
394 bind(&ParagraphList::push_back, ref(pl), _1));
398 void InsetText::addPreview(PreviewLoader & loader) const
400 ParagraphList::const_iterator pit = paragraphs().begin();
401 ParagraphList::const_iterator pend = paragraphs().end();
403 for (; pit != pend; ++pit) {
404 InsetList::const_iterator it = pit->insetList().begin();
405 InsetList::const_iterator end = pit->insetList().end();
406 for (; it != end; ++it)
407 it->inset->addPreview(loader);
412 ParagraphList const & InsetText::paragraphs() const
414 return text_.paragraphs();
418 ParagraphList & InsetText::paragraphs()
420 return text_.paragraphs();
424 //void InsetInclude::addToToc(ParConstIterator const & cpit) const
429 void InsetText::updateLabels(ParIterator const & it)
431 ParIterator it2 = it;
433 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
434 lyx::updateLabels(buffer(), it2);
438 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
440 if (cur.buffer().isClean())
441 return Inset::notifyCursorLeaves(old, cur);
443 // find text inset in old cursor
444 Cursor insetCur = old;
445 int scriptSlice = insetCur.find(this);
446 BOOST_ASSERT(scriptSlice != -1);
447 insetCur.cutOff(scriptSlice);
448 BOOST_ASSERT(&insetCur.inset() == this);
450 // update the old paragraph's words
451 insetCur.paragraph().updateWords(insetCur.top());
453 return Inset::notifyCursorLeaves(old, cur);
457 bool InsetText::completionSupported(Cursor const & cur) const
459 Cursor const & bvCur = cur.bv().cursor();
460 if (&bvCur.inset() != this)
462 return text_.completionSupported(cur);
466 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
468 return completionSupported(cur);
472 bool InsetText::automaticInlineCompletion() const
474 return lyxrc.completion_inline_text;
478 bool InsetText::automaticPopupCompletion() const
480 return lyxrc.completion_popup_text;
484 bool InsetText::showCompletionCursor() const
486 return lyxrc.completion_cursor_text;
490 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
492 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
496 docstring InsetText::completionPrefix(Cursor const & cur) const
498 if (!completionSupported(cur))
500 return text_.completionPrefix(cur);
504 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
507 if (!completionSupported(cur))
510 return text_.insertCompletion(cur, s, finished);
514 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
515 Dimension & dim) const
517 TextMetrics const & tm = cur.bv().textMetrics(&text_);
518 tm.completionPosAndDim(cur, x, y, dim);
522 docstring InsetText::contextMenu(BufferView const &, int, int) const
524 return from_ascii("context-edit");