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 "BufferParams.h"
18 #include "BufferView.h"
19 #include "CoordCache.h"
20 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
24 #include "ErrorList.h"
25 #include "FuncRequest.h"
33 #include "MetricsInfo.h"
34 #include "OutputParams.h"
35 #include "output_docbook.h"
36 #include "output_latex.h"
37 #include "output_plaintext.h"
38 #include "Paragraph.h"
39 #include "paragraph_funcs.h"
40 #include "ParagraphParameters.h"
41 #include "rowpainter.h"
47 #include "frontends/alert.h"
48 #include "frontends/Painter.h"
50 #include "support/lyxalgo.h" // count
52 #include <boost/bind.hpp>
53 #include <boost/current_function.hpp>
60 using graphics::PreviewLoader;
62 using support::isStrUnsignedInt;
76 int InsetText::border_ = 2;
79 InsetText::InsetText(BufferParams const & bp)
80 : drawFrame_(false), frame_color_(Color::insetframe)
82 paragraphs().push_back(Paragraph());
83 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
84 // Dispose of the infamous L-shaped cursor.
85 text_.current_font.setLanguage(bp.language);
86 text_.real_current_font.setLanguage(bp.language);
91 InsetText::InsetText(InsetText const & in)
92 : InsetBase(in), text_()
94 text_.autoBreakRows_ = in.text_.autoBreakRows_;
95 drawFrame_ = in.drawFrame_;
96 frame_color_ = in.frame_color_;
97 text_.paragraphs() = in.text_.paragraphs();
98 // Hand current buffer language down to "cloned" textinsets
100 text_.current_font = in.text_.current_font;
101 text_.real_current_font = in.text_.real_current_font;
106 InsetText::InsetText()
110 void InsetText::init()
112 for_each(paragraphs().begin(), paragraphs().end(),
113 bind(&Paragraph::setInsetOwner, _1, this));
117 void InsetText::clear()
119 ParagraphList & pars = paragraphs();
121 // This is a gross hack...
122 LyXLayout_ptr old_layout = pars.begin()->layout();
125 pars.push_back(Paragraph());
126 pars.begin()->setInsetOwner(this);
127 pars.begin()->layout(old_layout);
131 auto_ptr<InsetBase> InsetText::doClone() const
133 return auto_ptr<InsetBase>(new InsetText(*this));
137 void InsetText::write(Buffer const & buf, ostream & os) const
140 text_.write(buf, os);
144 void InsetText::read(Buffer const & buf, Lexer & lex)
148 // delete the initial paragraph
149 Paragraph oldpar = *paragraphs().begin();
150 paragraphs().clear();
152 bool res = text_.read(buf, lex, errorList);
156 lex.printError("Missing \\end_inset at this point. "
161 // ensure we have at least one paragraph.
162 if (paragraphs().empty())
163 paragraphs().push_back(oldpar);
167 bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
169 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
171 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
172 mi.base.textwidth -= 2 * border_;
173 font_ = mi.base.font;
174 // Hand font through to contained lyxtext:
175 text_.font_ = mi.base.font;
179 dim.wid += 2 * border_;
180 mi.base.textwidth += 2 * border_;
181 bool const changed = dim_ != dim;
187 void InsetText::draw(PainterInfo & pi, int x, int y) const
189 // update our idea of where we are
190 setPosCache(pi, x, y);
192 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
194 text_.background_color_ = backgroundColor();
195 text_.draw(pi, x + border_, y);
198 int const w = tm.width() + 2 * border_;
199 int const a = tm.ascent() + border_;
200 int const h = a + tm.descent() + border_;
201 pi.pain.rectangle(x, y - a,
202 ((wide() || hasFixedWidth()) ? tm.maxWidth() : w),
208 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
210 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
212 int const w = tm.width() + 2 * border_;
213 int const a = tm.ascent() + border_;
214 int const h = a + tm.descent() + border_;
215 pi.pain.fillRectangle(x, y - a,
216 ((wide() || hasFixedWidth()) ? tm.maxWidth() : w),
217 h, backgroundColor());
218 text_.drawSelection(pi, x + border_, y);
222 bool InsetText::covers(BufferView const & bv, int x, int y) const
224 TextMetrics const & tm = bv.textMetrics(&text_);
226 return bv.coordCache().getInsets().has(this)
228 && x <= xo(bv) + width() + (wide() ? tm.maxWidth() : 0)
229 && y >= yo(bv) - ascent()
230 && y <= yo(bv) + descent();
234 docstring const InsetText::editMessage() const
236 return _("Opened Text Inset");
240 void InsetText::edit(Cursor & cur, bool left)
242 //lyxerr << "InsetText: edit left/right" << endl;
243 int const pit = left ? 0 : paragraphs().size() - 1;
244 int const pos = left ? 0 : paragraphs().back().size();
245 text_.setCursor(cur.top(), pit, pos);
246 cur.clearSelection();
251 InsetBase * InsetText::editXY(Cursor & cur, int x, int y)
253 return text_.editXY(cur, x, y);
257 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
259 LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
260 << " [ cmd.action = "
261 << cmd.action << ']' << endl;
262 text_.dispatch(cur, cmd);
266 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
267 FuncStatus & status) const
269 return text_.getStatus(cur, cmd, status);
273 void InsetText::setChange(Change const & change)
275 ParagraphList::iterator pit = paragraphs().begin();
276 ParagraphList::iterator end = paragraphs().end();
277 for (; pit != end; ++pit) {
278 pit->setChange(change);
283 void InsetText::acceptChanges(BufferParams const & bparams)
285 text_.acceptChanges(bparams);
289 void InsetText::rejectChanges(BufferParams const & bparams)
291 text_.rejectChanges(bparams);
295 int InsetText::latex(Buffer const & buf, odocstream & os,
296 OutputParams const & runparams) const
299 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
300 return texrow.rows();
304 int InsetText::plaintext(Buffer const & buf, odocstream & os,
305 OutputParams const & runparams) const
307 ParagraphList::const_iterator beg = paragraphs().begin();
308 ParagraphList::const_iterator end = paragraphs().end();
309 ParagraphList::const_iterator it = beg;
310 bool ref_printed = false;
312 for (; it != end; ++it) {
315 if (runparams.linelen > 0)
318 odocstringstream oss;
319 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
320 docstring const str = oss.str();
322 // FIXME: len is not computed fully correctly; in principle,
323 // we have to count the characters after the last '\n'
331 int InsetText::docbook(Buffer const & buf, odocstream & os,
332 OutputParams const & runparams) const
334 docbookParagraphs(paragraphs(), buf, os, runparams);
339 void InsetText::validate(LaTeXFeatures & features) const
341 for_each(paragraphs().begin(), paragraphs().end(),
342 bind(&Paragraph::validate, _1, ref(features)));
346 void InsetText::cursorPos(BufferView const & bv,
347 CursorSlice const & sl, bool boundary, int & x, int & y) const
349 x = text_.cursorX(bv, sl, boundary) + border_;
350 y = text_.cursorY(bv, sl, boundary);
354 bool InsetText::showInsetDialog(BufferView *) const
360 void InsetText::setText(docstring const & data, LyXFont const & font, bool trackChanges)
363 Paragraph & first = paragraphs().front();
364 for (unsigned int i = 0; i < data.length(); ++i)
365 first.insertChar(i, data[i], font, trackChanges);
369 void InsetText::setAutoBreakRows(bool flag)
371 if (flag == text_.autoBreakRows_)
374 text_.autoBreakRows_ = flag;
378 // remove previously existing newlines
379 ParagraphList::iterator it = paragraphs().begin();
380 ParagraphList::iterator end = paragraphs().end();
381 for (; it != end; ++it)
382 for (int i = 0; i < it->size(); ++i)
383 if (it->isNewline(i))
384 // do not track the change, because the user
385 // is not allowed to revert/reject it
386 it->eraseChar(i, false);
390 void InsetText::setDrawFrame(bool flag)
396 Color_color InsetText::frameColor() const
398 return Color::color(frame_color_);
402 void InsetText::setFrameColor(Color_color col)
408 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
410 // There is little we can do here to keep track of changes.
411 // As of 2006/10/20, appendParagraphs is used exclusively by
412 // LyXTabular::setMultiColumn. In this context, the paragraph break
413 // is lost irreversibly and the appended text doesn't really change
415 ParagraphList & pl = paragraphs();
417 ParagraphList::iterator pit = plist.begin();
418 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
420 mergeParagraph(buffer->params(), pl,
421 std::distance(pl.begin(), ins) - 1);
423 for_each(pit, plist.end(),
424 bind(&ParagraphList::push_back, ref(pl), _1));
428 void InsetText::addPreview(PreviewLoader & loader) const
430 ParagraphList::const_iterator pit = paragraphs().begin();
431 ParagraphList::const_iterator pend = paragraphs().end();
433 for (; pit != pend; ++pit) {
434 InsetList::const_iterator it = pit->insetlist.begin();
435 InsetList::const_iterator end = pit->insetlist.end();
436 for (; it != end; ++it)
437 it->inset->addPreview(loader);
442 //FIXME: instead of this hack, which only works by chance,
443 // cells should have their own insetcell type, which returns CELL_CODE!
444 bool InsetText::neverIndent(Buffer const & buffer) const
446 // this is only true for tabular cells
447 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
451 ParagraphList const & InsetText::paragraphs() const
453 return text_.paragraphs();
457 ParagraphList & InsetText::paragraphs()
459 return text_.paragraphs();