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"
48 #include "frontends/alert.h"
49 #include "frontends/Painter.h"
51 #include "support/lyxalgo.h" // count
53 #include <boost/bind.hpp>
54 #include <boost/current_function.hpp>
55 #include <boost/signal.hpp>
62 using graphics::PreviewLoader;
64 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());
89 InsetText::InsetText(InsetText const & in)
92 text_.autoBreakRows_ = in.text_.autoBreakRows_;
93 drawFrame_ = in.drawFrame_;
94 frame_color_ = in.frame_color_;
95 text_.paragraphs() = in.text_.paragraphs();
100 InsetText::InsetText()
104 void InsetText::init()
106 for_each(paragraphs().begin(), paragraphs().end(),
107 bind(&Paragraph::setInsetOwner, _1, this));
111 void InsetText::clear()
113 ParagraphList & pars = paragraphs();
115 // This is a gross hack...
116 LayoutPtr old_layout = pars.begin()->layout();
119 pars.push_back(Paragraph());
120 pars.begin()->setInsetOwner(this);
121 pars.begin()->layout(old_layout);
125 Inset * InsetText::clone() const
127 return new InsetText(*this);
131 void InsetText::write(Buffer const & buf, ostream & os) const
134 text_.write(buf, os);
138 void InsetText::read(Buffer const & buf, Lexer & lex)
142 // delete the initial paragraph
143 Paragraph oldpar = *paragraphs().begin();
144 paragraphs().clear();
146 bool res = text_.read(buf, lex, errorList);
150 lex.printError("Missing \\end_inset at this point. "
155 // ensure we have at least one paragraph.
156 if (paragraphs().empty())
157 paragraphs().push_back(oldpar);
161 bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
163 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
165 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
167 // Hand font through to contained lyxtext:
168 text_.font_ = mi.base.font;
169 mi.base.textwidth -= 2 * border_;
171 mi.base.textwidth += 2 * border_;
174 dim.wid += 2 * border_;
175 bool const changed = dim_ != dim;
181 void InsetText::draw(PainterInfo & pi, int x, int y) const
183 // update our idea of where we are
184 setPosCache(pi, x, y);
186 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
188 if (drawFrame_ || pi.full_repaint) {
189 int const w = hasFixedWidth() ?
190 tm.maxWidth() : tm.width() + 2 * border_;
191 int const a = tm.ascent() + border_;
192 int const h = a + tm.descent() + border_;
194 pi.pain.fillRectangle(x, y - a, w, h, backgroundColor());
196 pi.pain.rectangle(x, y - a, w, h, frameColor());
198 tm.drawSelection(pi, x + border_, y);
199 tm.draw(pi, x + border_, y);
203 docstring const InsetText::editMessage() const
205 return _("Opened Text Inset");
209 void InsetText::edit(Cursor & cur, bool left)
211 //lyxerr << "InsetText: edit left/right" << endl;
212 int const pit = left ? 0 : paragraphs().size() - 1;
213 int const pos = left ? 0 : paragraphs().back().size();
214 text_.setCursor(cur.top(), pit, pos);
215 cur.clearSelection();
220 Inset * InsetText::editXY(Cursor & cur, int x, int y)
222 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
226 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
228 LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
229 << " [ cmd.action = "
230 << cmd.action << ']' << endl;
231 text_.dispatch(cur, cmd);
235 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
236 FuncStatus & status) const
238 return text_.getStatus(cur, cmd, status);
242 void InsetText::setChange(Change const & change)
244 ParagraphList::iterator pit = paragraphs().begin();
245 ParagraphList::iterator end = paragraphs().end();
246 for (; pit != end; ++pit) {
247 pit->setChange(change);
252 void InsetText::acceptChanges(BufferParams const & bparams)
254 text_.acceptChanges(bparams);
258 void InsetText::rejectChanges(BufferParams const & bparams)
260 text_.rejectChanges(bparams);
264 int InsetText::latex(Buffer const & buf, odocstream & os,
265 OutputParams const & runparams) const
268 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
269 return texrow.rows();
273 int InsetText::plaintext(Buffer const & buf, odocstream & os,
274 OutputParams const & runparams) const
276 ParagraphList::const_iterator beg = paragraphs().begin();
277 ParagraphList::const_iterator end = paragraphs().end();
278 ParagraphList::const_iterator it = beg;
279 bool ref_printed = false;
281 for (; it != end; ++it) {
284 if (runparams.linelen > 0)
287 odocstringstream oss;
288 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
289 docstring const str = oss.str();
291 // FIXME: len is not computed fully correctly; in principle,
292 // we have to count the characters after the last '\n'
300 int InsetText::docbook(Buffer const & buf, odocstream & os,
301 OutputParams const & runparams) const
303 docbookParagraphs(paragraphs(), buf, os, runparams);
308 void InsetText::validate(LaTeXFeatures & features) const
310 for_each(paragraphs().begin(), paragraphs().end(),
311 bind(&Paragraph::validate, _1, ref(features)));
315 void InsetText::cursorPos(BufferView const & bv,
316 CursorSlice const & sl, bool boundary, int & x, int & y) const
318 x = bv.textMetrics(&text_).cursorX(sl, boundary) + border_;
319 y = bv.textMetrics(&text_).cursorY(sl, boundary);
323 bool InsetText::showInsetDialog(BufferView *) const
329 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
332 Paragraph & first = paragraphs().front();
333 for (unsigned int i = 0; i < data.length(); ++i)
334 first.insertChar(i, data[i], font, trackChanges);
338 void InsetText::setAutoBreakRows(bool flag)
340 if (flag == text_.autoBreakRows_)
343 text_.autoBreakRows_ = flag;
347 // remove previously existing newlines
348 ParagraphList::iterator it = paragraphs().begin();
349 ParagraphList::iterator end = paragraphs().end();
350 for (; it != end; ++it)
351 for (int i = 0; i < it->size(); ++i)
352 if (it->isNewline(i))
353 // do not track the change, because the user
354 // is not allowed to revert/reject it
355 it->eraseChar(i, false);
359 void InsetText::setDrawFrame(bool flag)
365 Color_color InsetText::frameColor() const
367 return Color::color(frame_color_);
371 void InsetText::setFrameColor(Color_color col)
377 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
379 // There is little we can do here to keep track of changes.
380 // As of 2006/10/20, appendParagraphs is used exclusively by
381 // LyXTabular::setMultiColumn. In this context, the paragraph break
382 // is lost irreversibly and the appended text doesn't really change
384 ParagraphList & pl = paragraphs();
386 ParagraphList::iterator pit = plist.begin();
387 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
389 mergeParagraph(buffer->params(), pl,
390 std::distance(pl.begin(), ins) - 1);
392 for_each(pit, plist.end(),
393 bind(&ParagraphList::push_back, ref(pl), _1));
397 void InsetText::addPreview(PreviewLoader & loader) const
399 ParagraphList::const_iterator pit = paragraphs().begin();
400 ParagraphList::const_iterator pend = paragraphs().end();
402 for (; pit != pend; ++pit) {
403 InsetList::const_iterator it = pit->insetlist.begin();
404 InsetList::const_iterator end = pit->insetlist.end();
405 for (; it != end; ++it)
406 it->inset->addPreview(loader);
411 //FIXME: instead of this hack, which only works by chance,
412 // cells should have their own insetcell type, which returns CELL_CODE!
413 bool InsetText::neverIndent(Buffer const & buffer) const
415 // this is only true for tabular cells
416 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
420 ParagraphList const & InsetText::paragraphs() const
422 return text_.paragraphs();
426 ParagraphList & InsetText::paragraphs()
428 return text_.paragraphs();
432 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
434 ParIterator it2 = it;
436 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
437 lyx::updateLabels(buf, it2);