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>
54 #include <boost/signal.hpp>
61 using graphics::PreviewLoader;
63 using support::isStrUnsignedInt;
77 int InsetText::border_ = 2;
80 InsetText::InsetText(BufferParams const & bp)
81 : drawFrame_(false), frame_color_(Color::insetframe)
83 paragraphs().push_back(Paragraph());
84 paragraphs().back().layout(bp.getTextClass().defaultLayout());
85 // Dispose of the infamous L-shaped cursor.
86 text_.current_font.setLanguage(bp.language);
87 text_.real_current_font.setLanguage(bp.language);
92 InsetText::InsetText(InsetText const & in)
95 text_.autoBreakRows_ = in.text_.autoBreakRows_;
96 drawFrame_ = in.drawFrame_;
97 frame_color_ = in.frame_color_;
98 text_.paragraphs() = in.text_.paragraphs();
99 // Hand current buffer language down to "cloned" textinsets
100 // e.g. tabular cells
101 text_.current_font = in.text_.current_font;
102 text_.real_current_font = in.text_.real_current_font;
107 InsetText::InsetText()
111 void InsetText::init()
113 for_each(paragraphs().begin(), paragraphs().end(),
114 bind(&Paragraph::setInsetOwner, _1, this));
118 void InsetText::clear()
120 ParagraphList & pars = paragraphs();
122 // This is a gross hack...
123 Layout_ptr old_layout = pars.begin()->layout();
126 pars.push_back(Paragraph());
127 pars.begin()->setInsetOwner(this);
128 pars.begin()->layout(old_layout);
132 auto_ptr<Inset> InsetText::doClone() const
134 return auto_ptr<Inset>(new InsetText(*this));
138 void InsetText::write(Buffer const & buf, ostream & os) const
141 text_.write(buf, os);
145 void InsetText::read(Buffer const & buf, Lexer & lex)
149 // delete the initial paragraph
150 Paragraph oldpar = *paragraphs().begin();
151 paragraphs().clear();
153 bool res = text_.read(buf, lex, errorList);
157 lex.printError("Missing \\end_inset at this point. "
162 // ensure we have at least one paragraph.
163 if (paragraphs().empty())
164 paragraphs().push_back(oldpar);
168 bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
170 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
172 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
173 mi.base.textwidth -= 2 * border_;
174 font_ = mi.base.font;
175 // Hand font through to contained lyxtext:
176 text_.font_ = mi.base.font;
180 dim.wid += 2 * border_;
181 mi.base.textwidth += 2 * border_;
182 bool const changed = dim_ != dim;
188 void InsetText::draw(PainterInfo & pi, int x, int y) const
190 // update our idea of where we are
191 setPosCache(pi, x, y);
193 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
195 text_.background_color_ = backgroundColor();
196 text_.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 ((wide() || 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 ((wide() || hasFixedWidth()) ? tm.maxWidth() : w),
218 h, backgroundColor());
219 text_.drawSelection(pi, x + border_, y);
223 bool InsetText::covers(BufferView const & bv, int x, int y) const
225 TextMetrics const & tm = bv.textMetrics(&text_);
227 return bv.coordCache().getInsets().has(this)
229 && x <= xo(bv) + width() + (wide() ? tm.maxWidth() : 0)
230 && y >= yo(bv) - ascent()
231 && y <= yo(bv) + descent();
235 docstring const InsetText::editMessage() const
237 return _("Opened Text Inset");
241 void InsetText::edit(Cursor & cur, bool left)
243 //lyxerr << "InsetText: edit left/right" << endl;
244 int const pit = left ? 0 : paragraphs().size() - 1;
245 int const pos = left ? 0 : paragraphs().back().size();
246 text_.setCursor(cur.top(), pit, pos);
247 cur.clearSelection();
252 Inset * InsetText::editXY(Cursor & cur, int x, int y)
254 return text_.editXY(cur, x, y);
258 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
260 LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
261 << " [ cmd.action = "
262 << cmd.action << ']' << endl;
263 text_.dispatch(cur, cmd);
267 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
268 FuncStatus & status) const
270 return text_.getStatus(cur, cmd, status);
274 void InsetText::setChange(Change const & change)
276 ParagraphList::iterator pit = paragraphs().begin();
277 ParagraphList::iterator end = paragraphs().end();
278 for (; pit != end; ++pit) {
279 pit->setChange(change);
284 void InsetText::acceptChanges(BufferParams const & bparams)
286 text_.acceptChanges(bparams);
290 void InsetText::rejectChanges(BufferParams const & bparams)
292 text_.rejectChanges(bparams);
296 int InsetText::latex(Buffer const & buf, odocstream & os,
297 OutputParams const & runparams) const
300 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
301 return texrow.rows();
305 int InsetText::plaintext(Buffer const & buf, odocstream & os,
306 OutputParams const & runparams) const
308 ParagraphList::const_iterator beg = paragraphs().begin();
309 ParagraphList::const_iterator end = paragraphs().end();
310 ParagraphList::const_iterator it = beg;
311 bool ref_printed = false;
313 for (; it != end; ++it) {
316 if (runparams.linelen > 0)
319 odocstringstream oss;
320 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
321 docstring const str = oss.str();
323 // FIXME: len is not computed fully correctly; in principle,
324 // we have to count the characters after the last '\n'
332 int InsetText::docbook(Buffer const & buf, odocstream & os,
333 OutputParams const & runparams) const
335 docbookParagraphs(paragraphs(), buf, os, runparams);
340 void InsetText::validate(LaTeXFeatures & features) const
342 for_each(paragraphs().begin(), paragraphs().end(),
343 bind(&Paragraph::validate, _1, ref(features)));
347 void InsetText::cursorPos(BufferView const & bv,
348 CursorSlice const & sl, bool boundary, int & x, int & y) const
350 x = text_.cursorX(bv, sl, boundary) + border_;
351 y = text_.cursorY(bv, sl, boundary);
355 bool InsetText::showInsetDialog(BufferView *) const
361 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
364 Paragraph & first = paragraphs().front();
365 for (unsigned int i = 0; i < data.length(); ++i)
366 first.insertChar(i, data[i], font, trackChanges);
370 void InsetText::setAutoBreakRows(bool flag)
372 if (flag == text_.autoBreakRows_)
375 text_.autoBreakRows_ = flag;
379 // remove previously existing newlines
380 ParagraphList::iterator it = paragraphs().begin();
381 ParagraphList::iterator end = paragraphs().end();
382 for (; it != end; ++it)
383 for (int i = 0; i < it->size(); ++i)
384 if (it->isNewline(i))
385 // do not track the change, because the user
386 // is not allowed to revert/reject it
387 it->eraseChar(i, false);
391 void InsetText::setDrawFrame(bool flag)
397 Color_color InsetText::frameColor() const
399 return Color::color(frame_color_);
403 void InsetText::setFrameColor(Color_color col)
409 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
411 // There is little we can do here to keep track of changes.
412 // As of 2006/10/20, appendParagraphs is used exclusively by
413 // LyXTabular::setMultiColumn. In this context, the paragraph break
414 // is lost irreversibly and the appended text doesn't really change
416 ParagraphList & pl = paragraphs();
418 ParagraphList::iterator pit = plist.begin();
419 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
421 mergeParagraph(buffer->params(), pl,
422 std::distance(pl.begin(), ins) - 1);
424 for_each(pit, plist.end(),
425 bind(&ParagraphList::push_back, ref(pl), _1));
429 void InsetText::addPreview(PreviewLoader & loader) const
431 ParagraphList::const_iterator pit = paragraphs().begin();
432 ParagraphList::const_iterator pend = paragraphs().end();
434 for (; pit != pend; ++pit) {
435 InsetList::const_iterator it = pit->insetlist.begin();
436 InsetList::const_iterator end = pit->insetlist.end();
437 for (; it != end; ++it)
438 it->inset->addPreview(loader);
443 //FIXME: instead of this hack, which only works by chance,
444 // cells should have their own insetcell type, which returns CELL_CODE!
445 bool InsetText::neverIndent(Buffer const & buffer) const
447 // this is only true for tabular cells
448 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
452 ParagraphList const & InsetText::paragraphs() const
454 return text_.paragraphs();
458 ParagraphList & InsetText::paragraphs()
460 return text_.paragraphs();