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"
23 #include "support/debug.h"
24 #include "DispatchResult.h"
25 #include "ErrorList.h"
26 #include "FuncRequest.h"
27 #include "support/gettext.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>
64 using graphics::PreviewLoader;
66 using support::isStrUnsignedInt;
69 InsetText::InsetText(BufferParams const & bp)
70 : drawFrame_(false), frame_color_(Color_insetframe)
72 paragraphs().push_back(Paragraph());
73 paragraphs().back().layout(bp.getTextClass().defaultLayout());
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 InsetText::InsetText()
93 void InsetText::init()
95 for_each(paragraphs().begin(), paragraphs().end(),
96 bind(&Paragraph::setInsetOwner, _1, this));
100 void InsetText::clear()
102 ParagraphList & pars = paragraphs();
104 // This is a gross hack...
105 LayoutPtr old_layout = pars.begin()->layout();
108 pars.push_back(Paragraph());
109 pars.begin()->setInsetOwner(this);
110 pars.begin()->layout(old_layout);
114 Inset * InsetText::clone() const
116 return new InsetText(*this);
120 Dimension const InsetText::dimension(BufferView const & bv) const
122 TextMetrics const & tm = bv.textMetrics(&text_);
123 Dimension dim = tm.dimension();
124 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
125 dim.des += TEXT_TO_INSET_OFFSET;
126 dim.asc += TEXT_TO_INSET_OFFSET;
131 void InsetText::write(Buffer const & buf, ostream & os) const
134 text_.write(buf, os);
138 void InsetText::read(Buffer const & buf, Lexer & lex)
142 // delete the initial paragraph
143 Paragraph oldpar = *paragraphs().begin();
144 paragraphs().clear();
146 bool res = text_.read(buf, lex, errorList);
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() + 2 * 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;
190 pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
192 pi.pain.rectangle(x, yframe, w, h, frameColor());
194 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
198 docstring const InsetText::editMessage() const
200 return _("Opened Text Inset");
204 void InsetText::edit(Cursor & cur, bool left)
206 //lyxerr << "InsetText: edit left/right" << endl;
207 int const pit = left ? 0 : paragraphs().size() - 1;
208 int const pos = left ? 0 : paragraphs().back().size();
209 text_.setCursor(cur.top(), pit, pos);
210 cur.clearSelection();
215 Inset * InsetText::editXY(Cursor & cur, int x, int y)
217 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
221 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
223 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
224 << " [ cmd.action = " << cmd.action << ']');
225 text_.dispatch(cur, cmd);
229 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
230 FuncStatus & status) const
232 return text_.getStatus(cur, cmd, status);
236 void InsetText::setChange(Change const & change)
238 ParagraphList::iterator pit = paragraphs().begin();
239 ParagraphList::iterator end = paragraphs().end();
240 for (; pit != end; ++pit) {
241 pit->setChange(change);
246 void InsetText::acceptChanges(BufferParams const & bparams)
248 text_.acceptChanges(bparams);
252 void InsetText::rejectChanges(BufferParams const & bparams)
254 text_.rejectChanges(bparams);
258 int InsetText::latex(Buffer const & buf, odocstream & os,
259 OutputParams const & runparams) const
262 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
263 return texrow.rows();
267 int InsetText::plaintext(Buffer const & buf, odocstream & os,
268 OutputParams const & runparams) const
270 ParagraphList::const_iterator beg = paragraphs().begin();
271 ParagraphList::const_iterator end = paragraphs().end();
272 ParagraphList::const_iterator it = beg;
273 bool ref_printed = false;
275 for (; it != end; ++it) {
278 if (runparams.linelen > 0)
281 odocstringstream oss;
282 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
283 docstring const str = oss.str();
285 // FIXME: len is not computed fully correctly; in principle,
286 // we have to count the characters after the last '\n'
294 int InsetText::docbook(Buffer const & buf, odocstream & os,
295 OutputParams const & runparams) const
297 docbookParagraphs(paragraphs(), buf, os, runparams);
302 void InsetText::validate(LaTeXFeatures & features) const
304 for_each(paragraphs().begin(), paragraphs().end(),
305 bind(&Paragraph::validate, _1, ref(features)));
309 void InsetText::cursorPos(BufferView const & bv,
310 CursorSlice const & sl, bool boundary, int & x, int & y) const
312 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
313 y = bv.textMetrics(&text_).cursorY(sl, boundary);
317 bool InsetText::showInsetDialog(BufferView *) const
323 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
326 Paragraph & first = paragraphs().front();
327 for (unsigned int i = 0; i < data.length(); ++i)
328 first.insertChar(i, data[i], font, trackChanges);
332 void InsetText::setAutoBreakRows(bool flag)
334 if (flag == text_.autoBreakRows_)
337 text_.autoBreakRows_ = flag;
341 // remove previously existing newlines
342 ParagraphList::iterator it = paragraphs().begin();
343 ParagraphList::iterator end = paragraphs().end();
344 for (; it != end; ++it)
345 for (int i = 0; i < it->size(); ++i)
346 if (it->isNewline(i))
347 // do not track the change, because the user
348 // is not allowed to revert/reject it
349 it->eraseChar(i, false);
353 void InsetText::setDrawFrame(bool flag)
359 ColorCode InsetText::frameColor() const
365 void InsetText::setFrameColor(ColorCode col)
371 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
373 // There is little we can do here to keep track of changes.
374 // As of 2006/10/20, appendParagraphs is used exclusively by
375 // LyXTabular::setMultiColumn. In this context, the paragraph break
376 // is lost irreversibly and the appended text doesn't really change
378 ParagraphList & pl = paragraphs();
380 ParagraphList::iterator pit = plist.begin();
381 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
383 mergeParagraph(buffer->params(), pl,
384 std::distance(pl.begin(), ins) - 1);
386 for_each(pit, plist.end(),
387 bind(&ParagraphList::push_back, ref(pl), _1));
391 void InsetText::addPreview(PreviewLoader & loader) const
393 ParagraphList::const_iterator pit = paragraphs().begin();
394 ParagraphList::const_iterator pend = paragraphs().end();
396 for (; pit != pend; ++pit) {
397 InsetList::const_iterator it = pit->insetList().begin();
398 InsetList::const_iterator end = pit->insetList().end();
399 for (; it != end; ++it)
400 it->inset->addPreview(loader);
405 //FIXME: instead of this hack, which only works by chance,
406 // cells should have their own insetcell type, which returns CELL_CODE!
407 bool InsetText::neverIndent(Buffer const & buffer) const
409 // this is only true for tabular cells
410 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
414 ParagraphList const & InsetText::paragraphs() const
416 return text_.paragraphs();
420 ParagraphList & InsetText::paragraphs()
422 return text_.paragraphs();
426 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
428 ParIterator it2 = it;
430 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
431 lyx::updateLabels(buf, it2);