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"
43 #include "rowpainter.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;
79 int InsetText::border_ = 2;
82 InsetText::InsetText(BufferParams const & bp)
83 : drawFrame_(false), frame_color_(Color::insetframe)
85 paragraphs().push_back(Paragraph());
86 paragraphs().back().layout(bp.getTextClass().defaultLayout());
87 // Dispose of the infamous L-shaped cursor.
88 text_.current_font.setLanguage(bp.language);
89 text_.real_current_font.setLanguage(bp.language);
94 InsetText::InsetText(InsetText const & in)
97 text_.autoBreakRows_ = in.text_.autoBreakRows_;
98 drawFrame_ = in.drawFrame_;
99 frame_color_ = in.frame_color_;
100 text_.paragraphs() = in.text_.paragraphs();
101 // Hand current buffer language down to "cloned" textinsets
102 // e.g. tabular cells
103 text_.current_font = in.text_.current_font;
104 text_.real_current_font = in.text_.real_current_font;
109 InsetText::InsetText()
113 void InsetText::init()
115 for_each(paragraphs().begin(), paragraphs().end(),
116 bind(&Paragraph::setInsetOwner, _1, this));
120 void InsetText::clear()
122 ParagraphList & pars = paragraphs();
124 // This is a gross hack...
125 Layout_ptr old_layout = pars.begin()->layout();
128 pars.push_back(Paragraph());
129 pars.begin()->setInsetOwner(this);
130 pars.begin()->layout(old_layout);
134 auto_ptr<Inset> InsetText::doClone() const
136 return auto_ptr<Inset>(new InsetText(*this));
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 bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
172 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
174 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
175 mi.base.textwidth -= 2 * border_;
176 font_ = mi.base.font;
177 // Hand font through to contained lyxtext:
178 text_.font_ = mi.base.font;
182 dim.wid += 2 * border_;
183 mi.base.textwidth += 2 * border_;
184 bool const changed = dim_ != dim;
190 void InsetText::draw(PainterInfo & pi, int x, int y) const
192 // update our idea of where we are
193 setPosCache(pi, x, y);
195 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
197 text_.background_color_ = backgroundColor();
198 text_.draw(pi, x + border_, y);
201 int const w = tm.width() + 2 * border_;
202 int const a = tm.ascent() + border_;
203 int const h = a + tm.descent() + border_;
204 pi.pain.rectangle(x, y - a,
205 ((wide() || hasFixedWidth()) ? tm.maxWidth() : w),
211 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
213 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
215 int const w = tm.width() + 2 * border_;
216 int const a = tm.ascent() + border_;
217 int const h = a + tm.descent() + border_;
218 pi.pain.fillRectangle(x, y - a,
219 ((wide() || hasFixedWidth()) ? tm.maxWidth() : w),
220 h, backgroundColor());
221 text_.drawSelection(pi, x + border_, y);
225 bool InsetText::covers(BufferView const & bv, int x, int y) const
227 TextMetrics const & tm = bv.textMetrics(&text_);
229 return bv.coordCache().getInsets().has(this)
231 && x <= xo(bv) + width() + (wide() ? tm.maxWidth() : 0)
232 && y >= yo(bv) - ascent()
233 && y <= yo(bv) + descent();
237 docstring const InsetText::editMessage() const
239 return _("Opened Text Inset");
243 void InsetText::edit(Cursor & cur, bool left)
245 //lyxerr << "InsetText: edit left/right" << endl;
246 int const pit = left ? 0 : paragraphs().size() - 1;
247 int const pos = left ? 0 : paragraphs().back().size();
248 text_.setCursor(cur.top(), pit, pos);
249 cur.clearSelection();
254 Inset * InsetText::editXY(Cursor & cur, int x, int y)
256 return text_.editXY(cur, x, y);
260 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
262 LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
263 << " [ cmd.action = "
264 << cmd.action << ']' << endl;
265 text_.dispatch(cur, cmd);
269 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
270 FuncStatus & status) const
272 return text_.getStatus(cur, cmd, status);
276 void InsetText::setChange(Change const & change)
278 ParagraphList::iterator pit = paragraphs().begin();
279 ParagraphList::iterator end = paragraphs().end();
280 for (; pit != end; ++pit) {
281 pit->setChange(change);
286 void InsetText::acceptChanges(BufferParams const & bparams)
288 text_.acceptChanges(bparams);
292 void InsetText::rejectChanges(BufferParams const & bparams)
294 text_.rejectChanges(bparams);
298 int InsetText::latex(Buffer const & buf, odocstream & os,
299 OutputParams const & runparams) const
302 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
303 return texrow.rows();
307 int InsetText::plaintext(Buffer const & buf, odocstream & os,
308 OutputParams const & runparams) const
310 ParagraphList::const_iterator beg = paragraphs().begin();
311 ParagraphList::const_iterator end = paragraphs().end();
312 ParagraphList::const_iterator it = beg;
313 bool ref_printed = false;
315 for (; it != end; ++it) {
318 if (runparams.linelen > 0)
321 odocstringstream oss;
322 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
323 docstring const str = oss.str();
325 // FIXME: len is not computed fully correctly; in principle,
326 // we have to count the characters after the last '\n'
334 int InsetText::docbook(Buffer const & buf, odocstream & os,
335 OutputParams const & runparams) const
337 docbookParagraphs(paragraphs(), buf, os, runparams);
342 void InsetText::validate(LaTeXFeatures & features) const
344 for_each(paragraphs().begin(), paragraphs().end(),
345 bind(&Paragraph::validate, _1, ref(features)));
349 bool InsetText::notifyCursorLeaves(Cursor & cur)
352 cur.updateFlags(cur.disp_.update() | Update::Force);
357 void InsetText::cursorPos(BufferView const & bv,
358 CursorSlice const & sl, bool boundary, int & x, int & y) const
360 x = text_.cursorX(bv, sl, boundary) + border_;
361 y = text_.cursorY(bv, sl, boundary);
365 bool InsetText::showInsetDialog(BufferView *) const
371 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
374 Paragraph & first = paragraphs().front();
375 for (unsigned int i = 0; i < data.length(); ++i)
376 first.insertChar(i, data[i], font, trackChanges);
380 void InsetText::setAutoBreakRows(bool flag)
382 if (flag == text_.autoBreakRows_)
385 text_.autoBreakRows_ = flag;
389 // remove previously existing newlines
390 ParagraphList::iterator it = paragraphs().begin();
391 ParagraphList::iterator end = paragraphs().end();
392 for (; it != end; ++it)
393 for (int i = 0; i < it->size(); ++i)
394 if (it->isNewline(i))
395 // do not track the change, because the user
396 // is not allowed to revert/reject it
397 it->eraseChar(i, false);
401 void InsetText::setDrawFrame(bool flag)
407 Color_color InsetText::frameColor() const
409 return Color::color(frame_color_);
413 void InsetText::setFrameColor(Color_color col)
419 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
421 // There is little we can do here to keep track of changes.
422 // As of 2006/10/20, appendParagraphs is used exclusively by
423 // LyXTabular::setMultiColumn. In this context, the paragraph break
424 // is lost irreversibly and the appended text doesn't really change
426 ParagraphList & pl = paragraphs();
428 ParagraphList::iterator pit = plist.begin();
429 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
431 mergeParagraph(buffer->params(), pl,
432 std::distance(pl.begin(), ins) - 1);
434 for_each(pit, plist.end(),
435 bind(&ParagraphList::push_back, ref(pl), _1));
439 void InsetText::addPreview(PreviewLoader & loader) const
441 ParagraphList::const_iterator pit = paragraphs().begin();
442 ParagraphList::const_iterator pend = paragraphs().end();
444 for (; pit != pend; ++pit) {
445 InsetList::const_iterator it = pit->insetlist.begin();
446 InsetList::const_iterator end = pit->insetlist.end();
447 for (; it != end; ++it)
448 it->inset->addPreview(loader);
453 //FIXME: instead of this hack, which only works by chance,
454 // cells should have their own insetcell type, which returns CELL_CODE!
455 bool InsetText::neverIndent(Buffer const & buffer) const
457 // this is only true for tabular cells
458 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
462 ParagraphList const & InsetText::paragraphs() const
464 return text_.paragraphs();
468 ParagraphList & InsetText::paragraphs()
470 return text_.paragraphs();
474 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
476 ParIterator it2 = it;
478 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
479 lyx::updateLabels(buf, it2);