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;
78 int InsetText::border_ = 2;
81 InsetText::InsetText(BufferParams const & bp)
82 : drawFrame_(false), frame_color_(Color::insetframe)
84 paragraphs().push_back(Paragraph());
85 paragraphs().back().layout(bp.getTextClass().defaultLayout());
86 // Dispose of the infamous L-shaped cursor.
87 text_.current_font.setLanguage(bp.language);
88 text_.real_current_font.setLanguage(bp.language);
93 InsetText::InsetText(InsetText const & in)
96 text_.autoBreakRows_ = in.text_.autoBreakRows_;
97 drawFrame_ = in.drawFrame_;
98 frame_color_ = in.frame_color_;
99 text_.paragraphs() = in.text_.paragraphs();
100 // Hand current buffer language down to "cloned" textinsets
101 // e.g. tabular cells
102 text_.current_font = in.text_.current_font;
103 text_.real_current_font = in.text_.real_current_font;
108 InsetText::InsetText()
112 void InsetText::init()
114 for_each(paragraphs().begin(), paragraphs().end(),
115 bind(&Paragraph::setInsetOwner, _1, this));
119 void InsetText::clear()
121 ParagraphList & pars = paragraphs();
123 // This is a gross hack...
124 LayoutPtr old_layout = pars.begin()->layout();
127 pars.push_back(Paragraph());
128 pars.begin()->setInsetOwner(this);
129 pars.begin()->layout(old_layout);
133 auto_ptr<Inset> InsetText::doClone() const
135 return auto_ptr<Inset>(new InsetText(*this));
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 bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
171 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
173 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
174 mi.base.textwidth -= 2 * border_;
175 font_ = mi.base.font;
176 // Hand font through to contained lyxtext:
177 text_.font_ = mi.base.font;
181 dim.wid += 2 * border_;
182 mi.base.textwidth += 2 * border_;
183 bool const changed = dim_ != dim;
189 void InsetText::draw(PainterInfo & pi, int x, int y) const
191 // update our idea of where we are
192 setPosCache(pi, x, y);
194 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
196 tm.draw(pi, x + border_, y);
199 int const w = tm.width() + 2 * border_;
200 int const a = tm.ascent() + border_;
201 int const h = a + tm.descent() + border_;
202 pi.pain.rectangle(x, y - a,
203 (hasFixedWidth() ? tm.maxWidth() : w),
209 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
211 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
213 int const w = tm.width() + 2 * border_;
214 int const a = tm.ascent() + border_;
215 int const h = a + tm.descent() + border_;
216 pi.pain.fillRectangle(x, y - a,
217 (hasFixedWidth() ? tm.maxWidth() : w),
218 h, backgroundColor());
219 text_.drawSelection(pi, x + border_, y);
223 docstring const InsetText::editMessage() const
225 return _("Opened Text Inset");
229 void InsetText::edit(Cursor & cur, bool left)
231 //lyxerr << "InsetText: edit left/right" << endl;
232 int const pit = left ? 0 : paragraphs().size() - 1;
233 int const pos = left ? 0 : paragraphs().back().size();
234 text_.setCursor(cur.top(), pit, pos);
235 cur.clearSelection();
240 Inset * InsetText::editXY(Cursor & cur, int x, int y)
242 return text_.editXY(cur, x, y);
246 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
248 LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
249 << " [ cmd.action = "
250 << cmd.action << ']' << endl;
251 text_.dispatch(cur, cmd);
255 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
256 FuncStatus & status) const
258 return text_.getStatus(cur, cmd, status);
262 void InsetText::setChange(Change const & change)
264 ParagraphList::iterator pit = paragraphs().begin();
265 ParagraphList::iterator end = paragraphs().end();
266 for (; pit != end; ++pit) {
267 pit->setChange(change);
272 void InsetText::acceptChanges(BufferParams const & bparams)
274 text_.acceptChanges(bparams);
278 void InsetText::rejectChanges(BufferParams const & bparams)
280 text_.rejectChanges(bparams);
284 int InsetText::latex(Buffer const & buf, odocstream & os,
285 OutputParams const & runparams) const
288 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
289 return texrow.rows();
293 int InsetText::plaintext(Buffer const & buf, odocstream & os,
294 OutputParams const & runparams) const
296 ParagraphList::const_iterator beg = paragraphs().begin();
297 ParagraphList::const_iterator end = paragraphs().end();
298 ParagraphList::const_iterator it = beg;
299 bool ref_printed = false;
301 for (; it != end; ++it) {
304 if (runparams.linelen > 0)
307 odocstringstream oss;
308 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
309 docstring const str = oss.str();
311 // FIXME: len is not computed fully correctly; in principle,
312 // we have to count the characters after the last '\n'
320 int InsetText::docbook(Buffer const & buf, odocstream & os,
321 OutputParams const & runparams) const
323 docbookParagraphs(paragraphs(), buf, os, runparams);
328 void InsetText::validate(LaTeXFeatures & features) const
330 for_each(paragraphs().begin(), paragraphs().end(),
331 bind(&Paragraph::validate, _1, ref(features)));
335 void InsetText::cursorPos(BufferView const & bv,
336 CursorSlice const & sl, bool boundary, int & x, int & y) const
338 x = text_.cursorX(bv, sl, boundary) + border_;
339 y = text_.cursorY(bv, sl, boundary);
343 bool InsetText::showInsetDialog(BufferView *) const
349 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
352 Paragraph & first = paragraphs().front();
353 for (unsigned int i = 0; i < data.length(); ++i)
354 first.insertChar(i, data[i], font, trackChanges);
358 void InsetText::setAutoBreakRows(bool flag)
360 if (flag == text_.autoBreakRows_)
363 text_.autoBreakRows_ = flag;
367 // remove previously existing newlines
368 ParagraphList::iterator it = paragraphs().begin();
369 ParagraphList::iterator end = paragraphs().end();
370 for (; it != end; ++it)
371 for (int i = 0; i < it->size(); ++i)
372 if (it->isNewline(i))
373 // do not track the change, because the user
374 // is not allowed to revert/reject it
375 it->eraseChar(i, false);
379 void InsetText::setDrawFrame(bool flag)
385 Color_color InsetText::frameColor() const
387 return Color::color(frame_color_);
391 void InsetText::setFrameColor(Color_color col)
397 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
399 // There is little we can do here to keep track of changes.
400 // As of 2006/10/20, appendParagraphs is used exclusively by
401 // LyXTabular::setMultiColumn. In this context, the paragraph break
402 // is lost irreversibly and the appended text doesn't really change
404 ParagraphList & pl = paragraphs();
406 ParagraphList::iterator pit = plist.begin();
407 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
409 mergeParagraph(buffer->params(), pl,
410 std::distance(pl.begin(), ins) - 1);
412 for_each(pit, plist.end(),
413 bind(&ParagraphList::push_back, ref(pl), _1));
417 void InsetText::addPreview(PreviewLoader & loader) const
419 ParagraphList::const_iterator pit = paragraphs().begin();
420 ParagraphList::const_iterator pend = paragraphs().end();
422 for (; pit != pend; ++pit) {
423 InsetList::const_iterator it = pit->insetlist.begin();
424 InsetList::const_iterator end = pit->insetlist.end();
425 for (; it != end; ++it)
426 it->inset->addPreview(loader);
431 //FIXME: instead of this hack, which only works by chance,
432 // cells should have their own insetcell type, which returns CELL_CODE!
433 bool InsetText::neverIndent(Buffer const & buffer) const
435 // this is only true for tabular cells
436 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
440 ParagraphList const & InsetText::paragraphs() const
442 return text_.paragraphs();
446 ParagraphList & InsetText::paragraphs()
448 return text_.paragraphs();
452 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
454 ParIterator it2 = it;
456 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
457 lyx::updateLabels(buf, it2);