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"
14 #include "InsetNewline.h"
17 #include "buffer_funcs.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "CoordCache.h"
21 #include "CutAndPaste.h"
24 #include "DispatchResult.h"
25 #include "ErrorList.h"
26 #include "FuncRequest.h"
28 #include "InsetList.h"
34 #include "MetricsInfo.h"
35 #include "OutputParams.h"
36 #include "output_docbook.h"
37 #include "output_latex.h"
38 #include "output_plaintext.h"
39 #include "Paragraph.h"
40 #include "paragraph_funcs.h"
41 #include "ParagraphParameters.h"
42 #include "ParIterator.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
49 #include "frontends/alert.h"
50 #include "frontends/Painter.h"
52 #include "support/lstrings.h"
54 #include <boost/bind.hpp>
55 #include <boost/assert.hpp>
60 using graphics::PreviewLoader;
62 using support::isStrUnsignedInt;
75 InsetText::InsetText(BufferParams const & bp)
76 : drawFrame_(false), frame_color_(Color_insetframe)
78 paragraphs().push_back(Paragraph());
79 paragraphs().back().layout(bp.getTextClass().defaultLayout());
84 InsetText::InsetText(InsetText const & in)
87 text_.autoBreakRows_ = in.text_.autoBreakRows_;
88 drawFrame_ = in.drawFrame_;
89 frame_color_ = in.frame_color_;
90 text_.paragraphs() = in.text_.paragraphs();
95 InsetText::InsetText()
99 void InsetText::init()
101 for_each(paragraphs().begin(), paragraphs().end(),
102 bind(&Paragraph::setInsetOwner, _1, this));
106 void InsetText::clear()
108 ParagraphList & pars = paragraphs();
110 // This is a gross hack...
111 LayoutPtr old_layout = pars.begin()->layout();
114 pars.push_back(Paragraph());
115 pars.begin()->setInsetOwner(this);
116 pars.begin()->layout(old_layout);
120 Inset * InsetText::clone() const
122 return new InsetText(*this);
126 Dimension const InsetText::dimension(BufferView const & bv) const
128 TextMetrics const & tm = bv.textMetrics(&text_);
129 Dimension dim = tm.dimension();
130 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
131 dim.des += TEXT_TO_INSET_OFFSET;
132 dim.asc += TEXT_TO_INSET_OFFSET;
137 void InsetText::write(Buffer const & buf, ostream & os) const
140 text_.write(buf, os);
144 void InsetText::read(Buffer const & buf, Lexer & lex)
148 // delete the initial paragraph
149 Paragraph oldpar = *paragraphs().begin();
150 paragraphs().clear();
152 bool res = text_.read(buf, lex, errorList);
156 lex.printError("Missing \\end_inset at this point. "
161 // ensure we have at least one paragraph.
162 if (paragraphs().empty())
163 paragraphs().push_back(oldpar);
167 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
169 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
171 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
173 // Hand font through to contained lyxtext:
174 tm.font_.fontInfo() = mi.base.font;
175 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
177 tm.metrics(mi, dim, mi.base.textwidth);
180 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
181 dim.asc += TEXT_TO_INSET_OFFSET;
182 dim.des += TEXT_TO_INSET_OFFSET;
183 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
187 void InsetText::draw(PainterInfo & pi, int x, int y) const
189 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
191 if (drawFrame_ || pi.full_repaint) {
192 int const w = tm.width() + 2 * TEXT_TO_INSET_OFFSET;
193 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
194 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
196 pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
198 pi.pain.rectangle(x, yframe, w, h, frameColor());
200 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
204 docstring const InsetText::editMessage() const
206 return _("Opened Text Inset");
210 void InsetText::edit(Cursor & cur, bool left)
212 //lyxerr << "InsetText: edit left/right" << endl;
213 int const pit = left ? 0 : paragraphs().size() - 1;
214 int const pos = left ? 0 : paragraphs().back().size();
215 text_.setCursor(cur.top(), pit, pos);
216 cur.clearSelection();
221 Inset * InsetText::editXY(Cursor & cur, int x, int y)
223 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
227 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
229 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
230 << " [ cmd.action = " << cmd.action << ']');
231 text_.dispatch(cur, cmd);
235 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
236 FuncStatus & status) const
238 return text_.getStatus(cur, cmd, status);
242 void InsetText::setChange(Change const & change)
244 ParagraphList::iterator pit = paragraphs().begin();
245 ParagraphList::iterator end = paragraphs().end();
246 for (; pit != end; ++pit) {
247 pit->setChange(change);
252 void InsetText::acceptChanges(BufferParams const & bparams)
254 text_.acceptChanges(bparams);
258 void InsetText::rejectChanges(BufferParams const & bparams)
260 text_.rejectChanges(bparams);
264 int InsetText::latex(Buffer const & buf, odocstream & os,
265 OutputParams const & runparams) const
268 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
269 return texrow.rows();
273 int InsetText::plaintext(Buffer const & buf, odocstream & os,
274 OutputParams const & runparams) const
276 ParagraphList::const_iterator beg = paragraphs().begin();
277 ParagraphList::const_iterator end = paragraphs().end();
278 ParagraphList::const_iterator it = beg;
279 bool ref_printed = false;
281 for (; it != end; ++it) {
284 if (runparams.linelen > 0)
287 odocstringstream oss;
288 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
289 docstring const str = oss.str();
291 // FIXME: len is not computed fully correctly; in principle,
292 // we have to count the characters after the last '\n'
300 int InsetText::docbook(Buffer const & buf, odocstream & os,
301 OutputParams const & runparams) const
303 docbookParagraphs(paragraphs(), buf, os, runparams);
308 void InsetText::validate(LaTeXFeatures & features) const
310 for_each(paragraphs().begin(), paragraphs().end(),
311 bind(&Paragraph::validate, _1, ref(features)));
315 void InsetText::cursorPos(BufferView const & bv,
316 CursorSlice const & sl, bool boundary, int & x, int & y) const
318 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
319 y = bv.textMetrics(&text_).cursorY(sl, boundary);
323 bool InsetText::showInsetDialog(BufferView *) const
329 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
332 Paragraph & first = paragraphs().front();
333 for (unsigned int i = 0; i < data.length(); ++i)
334 first.insertChar(i, data[i], font, trackChanges);
338 void InsetText::setAutoBreakRows(bool flag)
340 if (flag == text_.autoBreakRows_)
343 text_.autoBreakRows_ = flag;
347 // remove previously existing newlines
348 ParagraphList::iterator it = paragraphs().begin();
349 ParagraphList::iterator end = paragraphs().end();
350 for (; it != end; ++it)
351 for (int i = 0; i < it->size(); ++i)
352 if (it->isNewline(i))
353 // do not track the change, because the user
354 // is not allowed to revert/reject it
355 it->eraseChar(i, false);
359 void InsetText::setDrawFrame(bool flag)
365 ColorCode InsetText::frameColor() const
371 void InsetText::setFrameColor(ColorCode col)
377 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
379 // There is little we can do here to keep track of changes.
380 // As of 2006/10/20, appendParagraphs is used exclusively by
381 // LyXTabular::setMultiColumn. In this context, the paragraph break
382 // is lost irreversibly and the appended text doesn't really change
384 ParagraphList & pl = paragraphs();
386 ParagraphList::iterator pit = plist.begin();
387 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
389 mergeParagraph(buffer->params(), pl,
390 std::distance(pl.begin(), ins) - 1);
392 for_each(pit, plist.end(),
393 bind(&ParagraphList::push_back, ref(pl), _1));
397 void InsetText::addPreview(PreviewLoader & loader) const
399 ParagraphList::const_iterator pit = paragraphs().begin();
400 ParagraphList::const_iterator pend = paragraphs().end();
402 for (; pit != pend; ++pit) {
403 InsetList::const_iterator it = pit->insetList().begin();
404 InsetList::const_iterator end = pit->insetList().end();
405 for (; it != end; ++it)
406 it->inset->addPreview(loader);
411 //FIXME: instead of this hack, which only works by chance,
412 // cells should have their own insetcell type, which returns CELL_CODE!
413 bool InsetText::neverIndent(Buffer const & buffer) const
415 // this is only true for tabular cells
416 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
420 ParagraphList const & InsetText::paragraphs() const
422 return text_.paragraphs();
426 ParagraphList & InsetText::paragraphs()
428 return text_.paragraphs();
432 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
434 ParIterator it2 = it;
436 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
437 lyx::updateLabels(buf, it2);