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"
23 #include "support/debug.h"
24 #include "DispatchResult.h"
25 #include "ErrorList.h"
26 #include "FuncRequest.h"
27 #include "support/gettext.h"
28 #include "InsetList.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"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
49 #include "frontends/alert.h"
50 #include "frontends/Painter.h"
52 #include "support/lstrings.h"
54 #include <boost/bind.hpp>
55 #include <boost/assert.hpp>
58 using namespace lyx::support;
65 using graphics::PreviewLoader;
68 InsetText::InsetText(BufferParams const & bp)
69 : drawFrame_(false), frame_color_(Color_insetframe)
71 paragraphs().push_back(Paragraph());
72 paragraphs().back().layout(bp.getTextClass().defaultLayout());
77 InsetText::InsetText(InsetText const & in)
80 text_.autoBreakRows_ = in.text_.autoBreakRows_;
81 drawFrame_ = in.drawFrame_;
82 frame_color_ = in.frame_color_;
83 text_.paragraphs() = in.text_.paragraphs();
88 InsetText::InsetText()
92 void InsetText::init()
94 for_each(paragraphs().begin(), paragraphs().end(),
95 bind(&Paragraph::setInsetOwner, _1, this));
99 void InsetText::clear()
101 ParagraphList & pars = paragraphs();
103 // This is a gross hack...
104 LayoutPtr old_layout = pars.begin()->layout();
107 pars.push_back(Paragraph());
108 pars.begin()->setInsetOwner(this);
109 pars.begin()->layout(old_layout);
113 Inset * InsetText::clone() const
115 return new InsetText(*this);
119 Dimension const InsetText::dimension(BufferView const & bv) const
121 TextMetrics const & tm = bv.textMetrics(&text_);
122 Dimension dim = tm.dimension();
123 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
124 dim.des += TEXT_TO_INSET_OFFSET;
125 dim.asc += TEXT_TO_INSET_OFFSET;
130 void InsetText::write(Buffer const & buf, ostream & os) const
133 text_.write(buf, os);
137 void InsetText::read(Buffer const & buf, Lexer & lex)
141 // delete the initial paragraph
142 Paragraph oldpar = *paragraphs().begin();
143 paragraphs().clear();
145 bool res = text_.read(buf, lex, errorList);
149 lex.printError("Missing \\end_inset at this point. "
154 // ensure we have at least one paragraph.
155 if (paragraphs().empty())
156 paragraphs().push_back(oldpar);
160 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
162 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
164 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
166 // Hand font through to contained lyxtext:
167 tm.font_.fontInfo() = mi.base.font;
168 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
170 tm.metrics(mi, dim, mi.base.textwidth);
173 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
174 dim.asc += TEXT_TO_INSET_OFFSET;
175 dim.des += TEXT_TO_INSET_OFFSET;
176 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
180 void InsetText::draw(PainterInfo & pi, int x, int y) const
182 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
184 if (drawFrame_ || pi.full_repaint) {
185 int const w = tm.width() + 2 * TEXT_TO_INSET_OFFSET;
186 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
187 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
189 pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
191 pi.pain.rectangle(x, yframe, w, h, frameColor());
193 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
197 docstring const InsetText::editMessage() const
199 return _("Opened Text Inset");
203 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
205 pit_type const pit = front ? 0 : paragraphs().size() - 1;
206 pos_type pos = front ? 0 : paragraphs().back().size();
208 // if visual information is not to be ignored, move to extreme right/left
209 if (entry_from != ENTRY_DIRECTION_IGNORE) {
210 Cursor temp_cur = cur;
211 temp_cur.pit() = pit;
212 temp_cur.pos() = pos;
213 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
214 pos = temp_cur.pos();
217 text_.setCursor(cur.top(), pit, pos);
218 cur.clearSelection();
223 Inset * InsetText::editXY(Cursor & cur, int x, int y)
225 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
229 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
231 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
232 << " [ cmd.action = " << cmd.action << ']');
233 text_.dispatch(cur, cmd);
237 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
238 FuncStatus & status) const
240 return text_.getStatus(cur, cmd, status);
244 void InsetText::setChange(Change const & change)
246 ParagraphList::iterator pit = paragraphs().begin();
247 ParagraphList::iterator end = paragraphs().end();
248 for (; pit != end; ++pit) {
249 pit->setChange(change);
254 void InsetText::acceptChanges(BufferParams const & bparams)
256 text_.acceptChanges(bparams);
260 void InsetText::rejectChanges(BufferParams const & bparams)
262 text_.rejectChanges(bparams);
266 int InsetText::latex(Buffer const & buf, odocstream & os,
267 OutputParams const & runparams) const
270 latexParagraphs(buf, text_, os, texrow, runparams);
271 return texrow.rows();
275 int InsetText::plaintext(Buffer const & buf, odocstream & os,
276 OutputParams const & runparams) const
278 ParagraphList::const_iterator beg = paragraphs().begin();
279 ParagraphList::const_iterator end = paragraphs().end();
280 ParagraphList::const_iterator it = beg;
281 bool ref_printed = false;
283 for (; it != end; ++it) {
286 if (runparams.linelen > 0)
289 odocstringstream oss;
290 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
291 docstring const str = oss.str();
293 // FIXME: len is not computed fully correctly; in principle,
294 // we have to count the characters after the last '\n'
302 int InsetText::docbook(Buffer const & buf, odocstream & os,
303 OutputParams const & runparams) const
305 docbookParagraphs(paragraphs(), buf, os, runparams);
310 void InsetText::validate(LaTeXFeatures & features) const
312 for_each(paragraphs().begin(), paragraphs().end(),
313 bind(&Paragraph::validate, _1, ref(features)));
317 void InsetText::cursorPos(BufferView const & bv,
318 CursorSlice const & sl, bool boundary, int & x, int & y) const
320 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
321 y = bv.textMetrics(&text_).cursorY(sl, boundary);
325 bool InsetText::showInsetDialog(BufferView *) const
331 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
334 Paragraph & first = paragraphs().front();
335 for (unsigned int i = 0; i < data.length(); ++i)
336 first.insertChar(i, data[i], font, trackChanges);
340 void InsetText::setAutoBreakRows(bool flag)
342 if (flag == text_.autoBreakRows_)
345 text_.autoBreakRows_ = flag;
349 // remove previously existing newlines
350 ParagraphList::iterator it = paragraphs().begin();
351 ParagraphList::iterator end = paragraphs().end();
352 for (; it != end; ++it)
353 for (int i = 0; i < it->size(); ++i)
354 if (it->isNewline(i))
355 // do not track the change, because the user
356 // is not allowed to revert/reject it
357 it->eraseChar(i, false);
361 void InsetText::setDrawFrame(bool flag)
367 ColorCode InsetText::frameColor() const
373 void InsetText::setFrameColor(ColorCode col)
379 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
381 // There is little we can do here to keep track of changes.
382 // As of 2006/10/20, appendParagraphs is used exclusively by
383 // LyXTabular::setMultiColumn. In this context, the paragraph break
384 // is lost irreversibly and the appended text doesn't really change
386 ParagraphList & pl = paragraphs();
388 ParagraphList::iterator pit = plist.begin();
389 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
391 mergeParagraph(buffer->params(), pl,
392 distance(pl.begin(), ins) - 1);
394 for_each(pit, plist.end(),
395 bind(&ParagraphList::push_back, ref(pl), _1));
399 void InsetText::addPreview(PreviewLoader & loader) const
401 ParagraphList::const_iterator pit = paragraphs().begin();
402 ParagraphList::const_iterator pend = paragraphs().end();
404 for (; pit != pend; ++pit) {
405 InsetList::const_iterator it = pit->insetList().begin();
406 InsetList::const_iterator end = pit->insetList().end();
407 for (; it != end; ++it)
408 it->inset->addPreview(loader);
413 //FIXME: instead of this hack, which only works by chance,
414 // cells should have their own insetcell type, which returns CELL_CODE!
415 bool InsetText::neverIndent(Buffer const & buffer) const
417 // this is only true for tabular cells
418 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
422 ParagraphList const & InsetText::paragraphs() const
424 return text_.paragraphs();
428 ParagraphList & InsetText::paragraphs()
430 return text_.paragraphs();
434 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
436 ParIterator it2 = it;
438 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
439 lyx::updateLabels(buf, it2);