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 "output_docbook.h"
35 #include "output_latex.h"
36 #include "output_plaintext.h"
37 #include "paragraph.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
40 #include "rowpainter.h"
46 #include "frontends/Alert.h"
47 #include "frontends/Painter.h"
49 #include "support/lyxalgo.h" // count
51 #include <boost/bind.hpp>
52 #include <boost/current_function.hpp>
59 using graphics::PreviewLoader;
61 using support::isStrUnsignedInt;
75 int InsetText::border_ = 2;
78 InsetText::InsetText(BufferParams const & bp)
79 : drawFrame_(false), frame_color_(LColor::insetframe)
81 paragraphs().push_back(Paragraph());
82 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
83 // Dispose of the infamous L-shaped cursor.
84 text_.current_font.setLanguage(bp.language);
85 text_.real_current_font.setLanguage(bp.language);
90 InsetText::InsetText(InsetText const & in)
91 : InsetOld(in), text_()
93 text_.autoBreakRows_ = in.text_.autoBreakRows_;
94 drawFrame_ = in.drawFrame_;
95 frame_color_ = in.frame_color_;
96 text_.paragraphs() = in.text_.paragraphs();
97 // Hand current buffer language down to "cloned" textinsets
99 text_.current_font = in.text_.current_font;
100 text_.real_current_font = in.text_.real_current_font;
105 InsetText::InsetText()
109 void InsetText::init()
111 for_each(paragraphs().begin(), paragraphs().end(),
112 bind(&Paragraph::setInsetOwner, _1, this));
116 void InsetText::clear()
118 ParagraphList & pars = paragraphs();
120 // This is a gross hack...
121 LyXLayout_ptr old_layout = pars.begin()->layout();
124 pars.push_back(Paragraph());
125 pars.begin()->setInsetOwner(this);
126 pars.begin()->layout(old_layout);
130 auto_ptr<InsetBase> InsetText::doClone() const
132 return auto_ptr<InsetBase>(new InsetText(*this));
136 void InsetText::write(Buffer const & buf, ostream & os) const
139 text_.write(buf, os);
143 void InsetText::read(Buffer const & buf, LyXLex & lex)
147 // delete the initial paragraph
148 Paragraph oldpar = *paragraphs().begin();
149 paragraphs().clear();
151 bool res = text_.read(buf, lex, errorList);
155 lex.printError("Missing \\end_inset at this point. "
160 // ensure we have at least one paragraph.
161 if (paragraphs().empty())
162 paragraphs().push_back(oldpar);
166 bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
168 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
170 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
171 mi.base.textwidth -= 2 * border_;
172 font_ = mi.base.font;
173 // Hand font through to contained lyxtext:
174 text_.font_ = mi.base.font;
178 dim.wid += 2 * border_;
179 mi.base.textwidth += 2 * border_;
180 bool const changed = dim_ != dim;
186 void InsetText::draw(PainterInfo & pi, int x, int y) const
188 // update our idea of where we are
189 setPosCache(pi, x, y);
191 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
193 text_.background_color_ = backgroundColor();
194 text_.draw(pi, x + border_, y);
197 int const w = tm.width() + 2 * border_;
198 int const a = tm.ascent() + border_;
199 int const h = a + tm.descent() + border_;
200 pi.pain.rectangle(x, y - a, (wide() ? tm.maxWidth() : w), h,
206 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
208 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
210 int const w = tm.width() + 2 * border_;
211 int const a = tm.ascent() + border_;
212 int const h = a + tm.descent() + border_;
213 pi.pain.fillRectangle(x, y - a, (wide() ? tm.maxWidth() : w), h,
215 text_.drawSelection(pi, x, y);
219 bool InsetText::covers(BufferView const & bv, int x, int y) const
221 TextMetrics const & tm = bv.textMetrics(&text_);
223 return bv.coordCache().getInsets().has(this)
225 && x <= xo(bv) + width() + (wide() ? tm.maxWidth() : 0)
226 && y >= yo(bv) - ascent()
227 && y <= yo(bv) + descent();
231 docstring const InsetText::editMessage() const
233 return _("Opened Text Inset");
237 void InsetText::edit(LCursor & cur, bool left)
239 //lyxerr << "InsetText: edit left/right" << endl;
240 int const pit = left ? 0 : paragraphs().size() - 1;
241 int const pos = left ? 0 : paragraphs().back().size();
242 text_.setCursor(cur.top(), pit, pos);
243 cur.clearSelection();
248 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
250 return text_.editXY(cur, x, y);
254 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
256 lyxerr[Debug::ACTION] << BOOST_CURRENT_FUNCTION
257 << " [ cmd.action = "
258 << cmd.action << ']' << endl;
259 text_.dispatch(cur, cmd);
263 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
264 FuncStatus & status) const
266 return text_.getStatus(cur, cmd, status);
270 void InsetText::setChange(Change const & change)
272 ParagraphList::iterator pit = paragraphs().begin();
273 ParagraphList::iterator end = paragraphs().end();
274 for (; pit != end; ++pit) {
275 pit->setChange(change);
280 void InsetText::acceptChanges(BufferParams const & bparams)
282 ParagraphList & pars = paragraphs();
284 // first, accept changes within each individual paragraph (do not consider end-of-par)
286 for (pit_type pit = 0; pit < pars.size(); ++pit) {
287 if (pars[pit].size() != 0) // prevent assertion failure
288 pars[pit].acceptChanges(bparams, 0, pars[pit].size());
291 // next, accept imaginary end-of-par characters
293 for (pit_type pit = 0; pit < pars.size(); ++pit) {
294 pos_type pos = pars[pit].size();
296 if (pars[pit].isInserted(pos)) {
297 pars[pit].setChange(pos, Change(Change::UNCHANGED));
298 } else if (pars[pit].isDeleted(pos)) {
299 if (pit == pars.size() - 1) {
300 // we cannot remove a par break at the end of the last paragraph;
301 // instead, we mark it unchanged
302 pars[pit].setChange(pos, Change(Change::UNCHANGED));
304 mergeParagraph(bparams, pars, pit);
310 // FIXME: finally, invoke the DEPM
314 void InsetText::rejectChanges(BufferParams const & bparams)
316 ParagraphList & pars = paragraphs();
318 // first, reject changes within each individual paragraph (do not consider end-of-par)
320 for (pit_type pit = 0; pit < pars.size(); ++pit) {
321 if (pars[pit].size() != 0) // prevent assertion failure
322 pars[pit].rejectChanges(bparams, 0, pars[pit].size());
325 // next, reject imaginary end-of-par characters
327 for (pit_type pit = 0; pit < pars.size(); ++pit) {
328 pos_type pos = pars[pit].size();
330 if (pars[pit].isDeleted(pos)) {
331 pars[pit].setChange(pos, Change(Change::UNCHANGED));
332 } else if (pars[pit].isInserted(pos)) {
333 if (pit == pars.size() - 1) {
334 // we mark the par break at the end of the last paragraph unchanged
335 pars[pit].setChange(pos, Change(Change::UNCHANGED));
337 mergeParagraph(bparams, pars, pit);
343 // FIXME: finally, invoke the DEPM
347 int InsetText::latex(Buffer const & buf, odocstream & os,
348 OutputParams const & runparams) const
351 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
352 return texrow.rows();
356 int InsetText::plaintext(Buffer const & buf, odocstream & os,
357 OutputParams const & runparams) const
359 ParagraphList::const_iterator beg = paragraphs().begin();
360 ParagraphList::const_iterator end = paragraphs().end();
361 ParagraphList::const_iterator it = beg;
362 bool ref_printed = false;
363 odocstringstream oss;
364 for (; it != end; ++it)
365 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
367 docstring const str = oss.str();
369 // Return how many newlines we issued.
370 return int(lyx::count(str.begin(), str.end(), '\n'));
374 int InsetText::docbook(Buffer const & buf, odocstream & os,
375 OutputParams const & runparams) const
377 docbookParagraphs(paragraphs(), buf, os, runparams);
382 void InsetText::validate(LaTeXFeatures & features) const
384 for_each(paragraphs().begin(), paragraphs().end(),
385 bind(&Paragraph::validate, _1, ref(features)));
389 void InsetText::cursorPos(BufferView const & bv,
390 CursorSlice const & sl, bool boundary, int & x, int & y) const
392 x = text_.cursorX(bv, sl, boundary) + border_;
393 y = text_.cursorY(bv, sl, boundary);
397 bool InsetText::showInsetDialog(BufferView *) const
403 void InsetText::setText(docstring const & data, LyXFont const & font, bool trackChanges)
406 Paragraph & first = paragraphs().front();
407 for (unsigned int i = 0; i < data.length(); ++i)
408 first.insertChar(i, data[i], font, trackChanges);
412 void InsetText::setAutoBreakRows(bool flag)
414 if (flag == text_.autoBreakRows_)
417 text_.autoBreakRows_ = flag;
421 // remove previously existing newlines
422 ParagraphList::iterator it = paragraphs().begin();
423 ParagraphList::iterator end = paragraphs().end();
424 for (; it != end; ++it)
425 for (int i = 0; i < it->size(); ++i)
426 if (it->isNewline(i))
427 // do not track the change, because the user
428 // is not allowed to revert/reject it
429 it->eraseChar(i, false);
433 void InsetText::setDrawFrame(bool flag)
439 LColor_color InsetText::frameColor() const
441 return LColor::color(frame_color_);
445 void InsetText::setFrameColor(LColor_color col)
451 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
453 // There is little we can do here to keep track of changes.
454 // As of 2006/10/20, appendParagraphs is used exclusively by
455 // LyXTabular::setMultiColumn. In this context, the paragraph break
456 // is lost irreversibly and the appended text doesn't really change
458 ParagraphList & pl = paragraphs();
460 ParagraphList::iterator pit = plist.begin();
461 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
463 mergeParagraph(buffer->params(), pl,
464 std::distance(pl.begin(), ins) - 1);
466 for_each(pit, plist.end(),
467 bind(&ParagraphList::push_back, ref(pl), _1));
471 void InsetText::addPreview(PreviewLoader & loader) const
473 ParagraphList::const_iterator pit = paragraphs().begin();
474 ParagraphList::const_iterator pend = paragraphs().end();
476 for (; pit != pend; ++pit) {
477 InsetList::const_iterator it = pit->insetlist.begin();
478 InsetList::const_iterator end = pit->insetlist.end();
479 for (; it != end; ++it)
480 it->inset->addPreview(loader);
485 //FIXME: instead of this hack, which only works by chance,
486 // cells should have their own insetcell type, which returns CELL_CODE!
487 bool InsetText::neverIndent(Buffer const & buffer) const
489 // this is only true for tabular cells
490 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
494 ParagraphList const & InsetText::paragraphs() const
496 return text_.paragraphs();
500 ParagraphList & InsetText::paragraphs()
502 return text_.paragraphs();