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_(LColor::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 : InsetOld(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, LyXLex & 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, (wide() ? tm.maxWidth() : w), h,
207 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
209 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
211 int const w = tm.width() + 2 * border_;
212 int const a = tm.ascent() + border_;
213 int const h = a + tm.descent() + border_;
214 pi.pain.fillRectangle(x, y - a, (wide() ? tm.maxWidth() : w), h,
216 text_.drawSelection(pi, x + border_, y);
220 bool InsetText::covers(BufferView const & bv, int x, int y) const
222 TextMetrics const & tm = bv.textMetrics(&text_);
224 return bv.coordCache().getInsets().has(this)
226 && x <= xo(bv) + width() + (wide() ? tm.maxWidth() : 0)
227 && y >= yo(bv) - ascent()
228 && y <= yo(bv) + descent();
232 docstring const InsetText::editMessage() const
234 return _("Opened Text Inset");
238 void InsetText::edit(LCursor & cur, bool left)
240 //lyxerr << "InsetText: edit left/right" << endl;
241 int const pit = left ? 0 : paragraphs().size() - 1;
242 int const pos = left ? 0 : paragraphs().back().size();
243 text_.setCursor(cur.top(), pit, pos);
244 cur.clearSelection();
249 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
251 return text_.editXY(cur, x, y);
255 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
257 lyxerr[Debug::ACTION] << BOOST_CURRENT_FUNCTION
258 << " [ cmd.action = "
259 << cmd.action << ']' << endl;
260 text_.dispatch(cur, cmd);
264 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
265 FuncStatus & status) const
267 return text_.getStatus(cur, cmd, status);
271 void InsetText::setChange(Change const & change)
273 ParagraphList::iterator pit = paragraphs().begin();
274 ParagraphList::iterator end = paragraphs().end();
275 for (; pit != end; ++pit) {
276 pit->setChange(change);
281 void InsetText::acceptChanges(BufferParams const & bparams)
283 text_.acceptChanges(bparams);
287 void InsetText::rejectChanges(BufferParams const & bparams)
289 text_.rejectChanges(bparams);
293 int InsetText::latex(Buffer const & buf, odocstream & os,
294 OutputParams const & runparams) const
297 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
298 return texrow.rows();
302 int InsetText::plaintext(Buffer const & buf, odocstream & os,
303 OutputParams const & runparams) const
305 ParagraphList::const_iterator beg = paragraphs().begin();
306 ParagraphList::const_iterator end = paragraphs().end();
307 ParagraphList::const_iterator it = beg;
308 bool ref_printed = false;
310 for (; it != end; ++it) {
313 if (runparams.linelen > 0)
316 odocstringstream oss;
317 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
318 docstring const str = oss.str();
320 // FIXME: len is not computed fully correctly; in principle,
321 // we have to count the characters after the last '\n'
329 int InsetText::docbook(Buffer const & buf, odocstream & os,
330 OutputParams const & runparams) const
332 docbookParagraphs(paragraphs(), buf, os, runparams);
337 void InsetText::validate(LaTeXFeatures & features) const
339 for_each(paragraphs().begin(), paragraphs().end(),
340 bind(&Paragraph::validate, _1, ref(features)));
344 void InsetText::cursorPos(BufferView const & bv,
345 CursorSlice const & sl, bool boundary, int & x, int & y) const
347 x = text_.cursorX(bv, sl, boundary) + border_;
348 y = text_.cursorY(bv, sl, boundary);
352 bool InsetText::showInsetDialog(BufferView *) const
358 void InsetText::setText(docstring const & data, LyXFont const & font, bool trackChanges)
361 Paragraph & first = paragraphs().front();
362 for (unsigned int i = 0; i < data.length(); ++i)
363 first.insertChar(i, data[i], font, trackChanges);
367 void InsetText::setAutoBreakRows(bool flag)
369 if (flag == text_.autoBreakRows_)
372 text_.autoBreakRows_ = flag;
376 // remove previously existing newlines
377 ParagraphList::iterator it = paragraphs().begin();
378 ParagraphList::iterator end = paragraphs().end();
379 for (; it != end; ++it)
380 for (int i = 0; i < it->size(); ++i)
381 if (it->isNewline(i))
382 // do not track the change, because the user
383 // is not allowed to revert/reject it
384 it->eraseChar(i, false);
388 void InsetText::setDrawFrame(bool flag)
394 LColor_color InsetText::frameColor() const
396 return LColor::color(frame_color_);
400 void InsetText::setFrameColor(LColor_color col)
406 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
408 // There is little we can do here to keep track of changes.
409 // As of 2006/10/20, appendParagraphs is used exclusively by
410 // LyXTabular::setMultiColumn. In this context, the paragraph break
411 // is lost irreversibly and the appended text doesn't really change
413 ParagraphList & pl = paragraphs();
415 ParagraphList::iterator pit = plist.begin();
416 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
418 mergeParagraph(buffer->params(), pl,
419 std::distance(pl.begin(), ins) - 1);
421 for_each(pit, plist.end(),
422 bind(&ParagraphList::push_back, ref(pl), _1));
426 void InsetText::addPreview(PreviewLoader & loader) const
428 ParagraphList::const_iterator pit = paragraphs().begin();
429 ParagraphList::const_iterator pend = paragraphs().end();
431 for (; pit != pend; ++pit) {
432 InsetList::const_iterator it = pit->insetlist.begin();
433 InsetList::const_iterator end = pit->insetlist.end();
434 for (; it != end; ++it)
435 it->inset->addPreview(loader);
440 //FIXME: instead of this hack, which only works by chance,
441 // cells should have their own insetcell type, which returns CELL_CODE!
442 bool InsetText::neverIndent(Buffer const & buffer) const
444 // this is only true for tabular cells
445 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
449 ParagraphList const & InsetText::paragraphs() const
451 return text_.paragraphs();
455 ParagraphList & InsetText::paragraphs()
457 return text_.paragraphs();