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>
58 using namespace lyx::support;
65 using graphics::PreviewLoader;
68 InsetText::InsetText(BufferParams const & bp)
69 : drawFrame_(false), frame_color_(Color_insetframe)
71 paragraphs().push_back(Paragraph());
72 paragraphs().back().layout(bp.getTextClass().defaultLayout());
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::init()
94 for_each(paragraphs().begin(), paragraphs().end(),
95 bind(&Paragraph::setInsetOwner, _1, this));
99 void InsetText::clear()
101 ParagraphList & pars = paragraphs();
103 // This is a gross hack...
104 LayoutPtr old_layout = pars.begin()->layout();
107 pars.push_back(Paragraph());
108 pars.begin()->setInsetOwner(this);
109 pars.begin()->layout(old_layout);
113 Inset * InsetText::clone() const
115 return new InsetText(*this);
119 Dimension const InsetText::dimension(BufferView const & bv) const
121 TextMetrics const & tm = bv.textMetrics(&text_);
122 Dimension dim = tm.dimension();
123 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
124 dim.des += TEXT_TO_INSET_OFFSET;
125 dim.asc += TEXT_TO_INSET_OFFSET;
130 void InsetText::write(Buffer const & buf, ostream & os) const
133 text_.write(buf, os);
137 void InsetText::read(Buffer const & buf, Lexer & lex)
141 // delete the initial paragraph
142 Paragraph oldpar = *paragraphs().begin();
143 paragraphs().clear();
145 bool res = text_.read(buf, lex, errorList);
149 lex.printError("Missing \\end_inset at this point. "
154 // ensure we have at least one paragraph.
155 if (paragraphs().empty())
156 paragraphs().push_back(oldpar);
160 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
162 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
164 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
166 // Hand font through to contained lyxtext:
167 tm.font_.fontInfo() = mi.base.font;
168 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
170 tm.metrics(mi, dim, mi.base.textwidth);
173 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
174 dim.asc += TEXT_TO_INSET_OFFSET;
175 dim.des += TEXT_TO_INSET_OFFSET;
176 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
180 void InsetText::draw(PainterInfo & pi, int x, int y) const
182 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
184 if (drawFrame_ || pi.full_repaint) {
185 int const w = tm.width() + 2 * TEXT_TO_INSET_OFFSET;
186 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
187 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
189 pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
191 pi.pain.rectangle(x, yframe, w, h, frameColor());
193 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
197 docstring const InsetText::editMessage() const
199 return _("Opened Text Inset");
203 void InsetText::edit(Cursor & cur, bool left)
205 //lyxerr << "InsetText: edit left/right" << endl;
206 int const pit = left ? 0 : paragraphs().size() - 1;
207 int const pos = left ? 0 : paragraphs().back().size();
208 text_.setCursor(cur.top(), pit, pos);
209 cur.clearSelection();
214 Inset * InsetText::editXY(Cursor & cur, int x, int y)
216 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
220 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
222 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
223 << " [ cmd.action = " << cmd.action << ']');
224 text_.dispatch(cur, cmd);
228 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
229 FuncStatus & status) const
231 return text_.getStatus(cur, cmd, status);
235 void InsetText::setChange(Change const & change)
237 ParagraphList::iterator pit = paragraphs().begin();
238 ParagraphList::iterator end = paragraphs().end();
239 for (; pit != end; ++pit) {
240 pit->setChange(change);
245 void InsetText::acceptChanges(BufferParams const & bparams)
247 text_.acceptChanges(bparams);
251 void InsetText::rejectChanges(BufferParams const & bparams)
253 text_.rejectChanges(bparams);
257 int InsetText::latex(Buffer const & buf, odocstream & os,
258 OutputParams const & runparams) const
261 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
262 return texrow.rows();
266 int InsetText::plaintext(Buffer const & buf, odocstream & os,
267 OutputParams const & runparams) const
269 ParagraphList::const_iterator beg = paragraphs().begin();
270 ParagraphList::const_iterator end = paragraphs().end();
271 ParagraphList::const_iterator it = beg;
272 bool ref_printed = false;
274 for (; it != end; ++it) {
277 if (runparams.linelen > 0)
280 odocstringstream oss;
281 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
282 docstring const str = oss.str();
284 // FIXME: len is not computed fully correctly; in principle,
285 // we have to count the characters after the last '\n'
293 int InsetText::docbook(Buffer const & buf, odocstream & os,
294 OutputParams const & runparams) const
296 docbookParagraphs(paragraphs(), buf, os, runparams);
301 void InsetText::validate(LaTeXFeatures & features) const
303 for_each(paragraphs().begin(), paragraphs().end(),
304 bind(&Paragraph::validate, _1, ref(features)));
308 void InsetText::cursorPos(BufferView const & bv,
309 CursorSlice const & sl, bool boundary, int & x, int & y) const
311 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
312 y = bv.textMetrics(&text_).cursorY(sl, boundary);
316 bool InsetText::showInsetDialog(BufferView *) const
322 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
325 Paragraph & first = paragraphs().front();
326 for (unsigned int i = 0; i < data.length(); ++i)
327 first.insertChar(i, data[i], font, trackChanges);
331 void InsetText::setAutoBreakRows(bool flag)
333 if (flag == text_.autoBreakRows_)
336 text_.autoBreakRows_ = flag;
340 // remove previously existing newlines
341 ParagraphList::iterator it = paragraphs().begin();
342 ParagraphList::iterator end = paragraphs().end();
343 for (; it != end; ++it)
344 for (int i = 0; i < it->size(); ++i)
345 if (it->isNewline(i))
346 // do not track the change, because the user
347 // is not allowed to revert/reject it
348 it->eraseChar(i, false);
352 void InsetText::setDrawFrame(bool flag)
358 ColorCode InsetText::frameColor() const
364 void InsetText::setFrameColor(ColorCode col)
370 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
372 // There is little we can do here to keep track of changes.
373 // As of 2006/10/20, appendParagraphs is used exclusively by
374 // LyXTabular::setMultiColumn. In this context, the paragraph break
375 // is lost irreversibly and the appended text doesn't really change
377 ParagraphList & pl = paragraphs();
379 ParagraphList::iterator pit = plist.begin();
380 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
382 mergeParagraph(buffer->params(), pl,
383 std::distance(pl.begin(), ins) - 1);
385 for_each(pit, plist.end(),
386 bind(&ParagraphList::push_back, ref(pl), _1));
390 void InsetText::addPreview(PreviewLoader & loader) const
392 ParagraphList::const_iterator pit = paragraphs().begin();
393 ParagraphList::const_iterator pend = paragraphs().end();
395 for (; pit != pend; ++pit) {
396 InsetList::const_iterator it = pit->insetList().begin();
397 InsetList::const_iterator end = pit->insetList().end();
398 for (; it != end; ++it)
399 it->inset->addPreview(loader);
404 //FIXME: instead of this hack, which only works by chance,
405 // cells should have their own insetcell type, which returns CELL_CODE!
406 bool InsetText::neverIndent(Buffer const & buffer) const
408 // this is only true for tabular cells
409 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
413 ParagraphList const & InsetText::paragraphs() const
415 return text_.paragraphs();
419 ParagraphList & InsetText::paragraphs()
421 return text_.paragraphs();
425 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
427 ParIterator it2 = it;
429 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
430 lyx::updateLabels(buf, it2);