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"
35 #include "MetricsInfo.h"
36 #include "OutputParams.h"
37 #include "output_docbook.h"
38 #include "output_latex.h"
39 #include "output_plaintext.h"
40 #include "Paragraph.h"
41 #include "paragraph_funcs.h"
42 #include "ParagraphParameters.h"
43 #include "ParIterator.h"
46 #include "TextMetrics.h"
49 #include "frontends/alert.h"
50 #include "frontends/Painter.h"
52 #include "support/lyxalgo.h" // count
54 #include <boost/bind.hpp>
55 #include <boost/current_function.hpp>
56 #include <boost/signal.hpp>
63 using graphics::PreviewLoader;
65 using support::isStrUnsignedInt;
78 InsetText::InsetText(BufferParams const & bp)
79 : drawFrame_(false), frame_color_(Color::insetframe)
81 paragraphs().push_back(Paragraph());
82 paragraphs().back().layout(bp.getTextClass().defaultLayout());
87 InsetText::InsetText(InsetText const & in)
90 text_.autoBreakRows_ = in.text_.autoBreakRows_;
91 drawFrame_ = in.drawFrame_;
92 frame_color_ = in.frame_color_;
93 text_.paragraphs() = in.text_.paragraphs();
98 InsetText::InsetText()
102 void InsetText::init()
104 for_each(paragraphs().begin(), paragraphs().end(),
105 bind(&Paragraph::setInsetOwner, _1, this));
109 void InsetText::clear()
111 ParagraphList & pars = paragraphs();
113 // This is a gross hack...
114 LayoutPtr old_layout = pars.begin()->layout();
117 pars.push_back(Paragraph());
118 pars.begin()->setInsetOwner(this);
119 pars.begin()->layout(old_layout);
123 Inset * InsetText::clone() const
125 return new InsetText(*this);
129 Dimension const InsetText::dimension(BufferView const & bv) const
131 TextMetrics const & tm = bv.textMetrics(&text_);
132 Dimension dim = tm.dimension();
133 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
134 dim.des += TEXT_TO_INSET_OFFSET;
135 dim.asc += TEXT_TO_INSET_OFFSET;
140 void InsetText::write(Buffer const & buf, ostream & os) const
143 text_.write(buf, os);
147 void InsetText::read(Buffer const & buf, Lexer & lex)
151 // delete the initial paragraph
152 Paragraph oldpar = *paragraphs().begin();
153 paragraphs().clear();
155 bool res = text_.read(buf, lex, errorList);
159 lex.printError("Missing \\end_inset at this point. "
164 // ensure we have at least one paragraph.
165 if (paragraphs().empty())
166 paragraphs().push_back(oldpar);
170 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
172 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
174 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
176 // Hand font through to contained lyxtext:
177 tm.font_ = mi.base.font;
178 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
180 tm.metrics(mi, dim, mi.base.textwidth);
183 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
184 dim.asc += TEXT_TO_INSET_OFFSET;
185 dim.des += TEXT_TO_INSET_OFFSET;
186 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
190 void InsetText::draw(PainterInfo & pi, int x, int y) const
192 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
194 if (drawFrame_ || pi.full_repaint) {
195 int const w = tm.width() + 2 * TEXT_TO_INSET_OFFSET;
196 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
197 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
199 pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
201 pi.pain.rectangle(x, yframe, w, h, frameColor());
203 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
207 docstring const InsetText::editMessage() const
209 return _("Opened Text Inset");
213 void InsetText::edit(Cursor & cur, bool left)
215 //lyxerr << "InsetText: edit left/right" << endl;
216 int const pit = left ? 0 : paragraphs().size() - 1;
217 int const pos = left ? 0 : paragraphs().back().size();
218 text_.setCursor(cur.top(), pit, pos);
219 cur.clearSelection();
224 Inset * InsetText::editXY(Cursor & cur, int x, int y)
226 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
230 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
232 LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
233 << " [ cmd.action = "
234 << cmd.action << ']' << endl;
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(Buffer const & buf, odocstream & os,
269 OutputParams const & runparams) const
272 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
273 return texrow.rows();
277 int InsetText::plaintext(Buffer const & buf, odocstream & os,
278 OutputParams const & runparams) const
280 ParagraphList::const_iterator beg = paragraphs().begin();
281 ParagraphList::const_iterator end = paragraphs().end();
282 ParagraphList::const_iterator it = beg;
283 bool ref_printed = false;
285 for (; it != end; ++it) {
288 if (runparams.linelen > 0)
291 odocstringstream oss;
292 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
293 docstring const str = oss.str();
295 // FIXME: len is not computed fully correctly; in principle,
296 // we have to count the characters after the last '\n'
304 int InsetText::docbook(Buffer const & buf, odocstream & os,
305 OutputParams const & runparams) const
307 docbookParagraphs(paragraphs(), buf, os, runparams);
312 void InsetText::validate(LaTeXFeatures & features) const
314 for_each(paragraphs().begin(), paragraphs().end(),
315 bind(&Paragraph::validate, _1, ref(features)));
319 void InsetText::cursorPos(BufferView const & bv,
320 CursorSlice const & sl, bool boundary, int & x, int & y) const
322 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
323 y = bv.textMetrics(&text_).cursorY(sl, boundary);
327 bool InsetText::showInsetDialog(BufferView *) const
333 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
336 Paragraph & first = paragraphs().front();
337 for (unsigned int i = 0; i < data.length(); ++i)
338 first.insertChar(i, data[i], font, trackChanges);
342 void InsetText::setAutoBreakRows(bool flag)
344 if (flag == text_.autoBreakRows_)
347 text_.autoBreakRows_ = flag;
351 // remove previously existing newlines
352 ParagraphList::iterator it = paragraphs().begin();
353 ParagraphList::iterator end = paragraphs().end();
354 for (; it != end; ++it)
355 for (int i = 0; i < it->size(); ++i)
356 if (it->isNewline(i))
357 // do not track the change, because the user
358 // is not allowed to revert/reject it
359 it->eraseChar(i, false);
363 void InsetText::setDrawFrame(bool flag)
369 Color_color InsetText::frameColor() const
371 return Color::color(frame_color_);
375 void InsetText::setFrameColor(Color_color col)
381 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
383 // There is little we can do here to keep track of changes.
384 // As of 2006/10/20, appendParagraphs is used exclusively by
385 // LyXTabular::setMultiColumn. In this context, the paragraph break
386 // is lost irreversibly and the appended text doesn't really change
388 ParagraphList & pl = paragraphs();
390 ParagraphList::iterator pit = plist.begin();
391 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
393 mergeParagraph(buffer->params(), pl,
394 std::distance(pl.begin(), ins) - 1);
396 for_each(pit, plist.end(),
397 bind(&ParagraphList::push_back, ref(pl), _1));
401 void InsetText::addPreview(PreviewLoader & loader) const
403 ParagraphList::const_iterator pit = paragraphs().begin();
404 ParagraphList::const_iterator pend = paragraphs().end();
406 for (; pit != pend; ++pit) {
407 InsetList::const_iterator it = pit->insetList().begin();
408 InsetList::const_iterator end = pit->insetList().end();
409 for (; it != end; ++it)
410 it->inset->addPreview(loader);
415 //FIXME: instead of this hack, which only works by chance,
416 // cells should have their own insetcell type, which returns CELL_CODE!
417 bool InsetText::neverIndent(Buffer const & buffer) const
419 // this is only true for tabular cells
420 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
424 ParagraphList const & InsetText::paragraphs() const
426 return text_.paragraphs();
430 ParagraphList & InsetText::paragraphs()
432 return text_.paragraphs();
436 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
438 ParIterator it2 = it;
440 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
441 lyx::updateLabels(buf, it2);