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"
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"
48 #include "frontends/alert.h"
49 #include "frontends/Painter.h"
51 #include "support/lyxalgo.h" // count
53 #include <boost/bind.hpp>
54 #include <boost/current_function.hpp>
55 #include <boost/signal.hpp>
62 using graphics::PreviewLoader;
64 using support::isStrUnsignedInt;
77 InsetText::InsetText(BufferParams const & bp)
78 : drawFrame_(false), frame_color_(Color::insetframe)
80 paragraphs().push_back(Paragraph());
81 paragraphs().back().layout(bp.getTextClass().defaultLayout());
86 InsetText::InsetText(InsetText const & in)
89 text_.autoBreakRows_ = in.text_.autoBreakRows_;
90 drawFrame_ = in.drawFrame_;
91 frame_color_ = in.frame_color_;
92 text_.paragraphs() = in.text_.paragraphs();
97 InsetText::InsetText()
101 void InsetText::init()
103 for_each(paragraphs().begin(), paragraphs().end(),
104 bind(&Paragraph::setInsetOwner, _1, this));
108 void InsetText::clear()
110 ParagraphList & pars = paragraphs();
112 // This is a gross hack...
113 LayoutPtr old_layout = pars.begin()->layout();
116 pars.push_back(Paragraph());
117 pars.begin()->setInsetOwner(this);
118 pars.begin()->layout(old_layout);
122 Inset * InsetText::clone() const
124 return new InsetText(*this);
128 Dimension const InsetText::dimension(BufferView const & bv) const
130 TextMetrics const & tm = bv.textMetrics(&text_);
131 Dimension dim = tm.dimension();
132 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
133 dim.des += TEXT_TO_INSET_OFFSET;
134 dim.asc += TEXT_TO_INSET_OFFSET;
139 void InsetText::write(Buffer const & buf, ostream & os) const
142 text_.write(buf, os);
146 void InsetText::read(Buffer const & buf, Lexer & lex)
150 // delete the initial paragraph
151 Paragraph oldpar = *paragraphs().begin();
152 paragraphs().clear();
154 bool res = text_.read(buf, lex, errorList);
158 lex.printError("Missing \\end_inset at this point. "
163 // ensure we have at least one paragraph.
164 if (paragraphs().empty())
165 paragraphs().push_back(oldpar);
169 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
171 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
173 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
175 // Hand font through to contained lyxtext:
176 tm.font_ = mi.base.font;
177 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
179 tm.metrics(mi, dim, mi.base.textwidth);
182 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
183 dim.asc += TEXT_TO_INSET_OFFSET;
184 dim.des += TEXT_TO_INSET_OFFSET;
185 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
189 void InsetText::draw(PainterInfo & pi, int x, int y) const
191 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
193 if (drawFrame_ || pi.full_repaint) {
194 int const w = tm.width() + 2 * TEXT_TO_INSET_OFFSET;
195 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
196 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
198 pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
200 pi.pain.rectangle(x, yframe, w, h, frameColor());
202 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
206 docstring const InsetText::editMessage() const
208 return _("Opened Text Inset");
212 void InsetText::edit(Cursor & cur, bool left)
214 //lyxerr << "InsetText: edit left/right" << endl;
215 int const pit = left ? 0 : paragraphs().size() - 1;
216 int const pos = left ? 0 : paragraphs().back().size();
217 text_.setCursor(cur.top(), pit, pos);
218 cur.clearSelection();
223 Inset * InsetText::editXY(Cursor & cur, int x, int y)
225 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
229 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
231 LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
232 << " [ cmd.action = "
233 << cmd.action << ']' << endl;
234 text_.dispatch(cur, cmd);
238 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
239 FuncStatus & status) const
241 return text_.getStatus(cur, cmd, status);
245 void InsetText::setChange(Change const & change)
247 ParagraphList::iterator pit = paragraphs().begin();
248 ParagraphList::iterator end = paragraphs().end();
249 for (; pit != end; ++pit) {
250 pit->setChange(change);
255 void InsetText::acceptChanges(BufferParams const & bparams)
257 text_.acceptChanges(bparams);
261 void InsetText::rejectChanges(BufferParams const & bparams)
263 text_.rejectChanges(bparams);
267 int InsetText::latex(Buffer const & buf, odocstream & os,
268 OutputParams const & runparams) const
271 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
272 return texrow.rows();
276 int InsetText::plaintext(Buffer const & buf, odocstream & os,
277 OutputParams const & runparams) const
279 ParagraphList::const_iterator beg = paragraphs().begin();
280 ParagraphList::const_iterator end = paragraphs().end();
281 ParagraphList::const_iterator it = beg;
282 bool ref_printed = false;
284 for (; it != end; ++it) {
287 if (runparams.linelen > 0)
290 odocstringstream oss;
291 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
292 docstring const str = oss.str();
294 // FIXME: len is not computed fully correctly; in principle,
295 // we have to count the characters after the last '\n'
303 int InsetText::docbook(Buffer const & buf, odocstream & os,
304 OutputParams const & runparams) const
306 docbookParagraphs(paragraphs(), buf, os, runparams);
311 void InsetText::validate(LaTeXFeatures & features) const
313 for_each(paragraphs().begin(), paragraphs().end(),
314 bind(&Paragraph::validate, _1, ref(features)));
318 void InsetText::cursorPos(BufferView const & bv,
319 CursorSlice const & sl, bool boundary, int & x, int & y) const
321 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
322 y = bv.textMetrics(&text_).cursorY(sl, boundary);
326 bool InsetText::showInsetDialog(BufferView *) const
332 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
335 Paragraph & first = paragraphs().front();
336 for (unsigned int i = 0; i < data.length(); ++i)
337 first.insertChar(i, data[i], font, trackChanges);
341 void InsetText::setAutoBreakRows(bool flag)
343 if (flag == text_.autoBreakRows_)
346 text_.autoBreakRows_ = flag;
350 // remove previously existing newlines
351 ParagraphList::iterator it = paragraphs().begin();
352 ParagraphList::iterator end = paragraphs().end();
353 for (; it != end; ++it)
354 for (int i = 0; i < it->size(); ++i)
355 if (it->isNewline(i))
356 // do not track the change, because the user
357 // is not allowed to revert/reject it
358 it->eraseChar(i, false);
362 void InsetText::setDrawFrame(bool flag)
368 Color_color InsetText::frameColor() const
370 return Color::color(frame_color_);
374 void InsetText::setFrameColor(Color_color col)
380 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
382 // There is little we can do here to keep track of changes.
383 // As of 2006/10/20, appendParagraphs is used exclusively by
384 // LyXTabular::setMultiColumn. In this context, the paragraph break
385 // is lost irreversibly and the appended text doesn't really change
387 ParagraphList & pl = paragraphs();
389 ParagraphList::iterator pit = plist.begin();
390 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
392 mergeParagraph(buffer->params(), pl,
393 std::distance(pl.begin(), ins) - 1);
395 for_each(pit, plist.end(),
396 bind(&ParagraphList::push_back, ref(pl), _1));
400 void InsetText::addPreview(PreviewLoader & loader) const
402 ParagraphList::const_iterator pit = paragraphs().begin();
403 ParagraphList::const_iterator pend = paragraphs().end();
405 for (; pit != pend; ++pit) {
406 InsetList::const_iterator it = pit->insetlist.begin();
407 InsetList::const_iterator end = pit->insetlist.end();
408 for (; it != end; ++it)
409 it->inset->addPreview(loader);
414 //FIXME: instead of this hack, which only works by chance,
415 // cells should have their own insetcell type, which returns CELL_CODE!
416 bool InsetText::neverIndent(Buffer const & buffer) const
418 // this is only true for tabular cells
419 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
423 ParagraphList const & InsetText::paragraphs() const
425 return text_.paragraphs();
429 ParagraphList & InsetText::paragraphs()
431 return text_.paragraphs();
435 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
437 ParIterator it2 = it;
439 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
440 lyx::updateLabels(buf, it2);