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());
73 paragraphs().back().layout(bp.getTextClass().emptyLayout());
75 paragraphs().back().layout(bp.getTextClass().defaultLayout());
80 InsetText::InsetText(InsetText const & in)
83 text_.autoBreakRows_ = in.text_.autoBreakRows_;
84 drawFrame_ = in.drawFrame_;
85 frame_color_ = in.frame_color_;
86 text_.paragraphs() = in.text_.paragraphs();
91 InsetText::InsetText()
95 void InsetText::init()
97 for_each(paragraphs().begin(), paragraphs().end(),
98 bind(&Paragraph::setInsetOwner, _1, this));
102 void InsetText::clear()
104 ParagraphList & pars = paragraphs();
106 // This is a gross hack...
107 LayoutPtr old_layout = pars.begin()->layout();
110 pars.push_back(Paragraph());
111 pars.begin()->setInsetOwner(this);
112 pars.begin()->layout(old_layout);
116 Inset * InsetText::clone() const
118 return new InsetText(*this);
122 Dimension const InsetText::dimension(BufferView const & bv) const
124 TextMetrics const & tm = bv.textMetrics(&text_);
125 Dimension dim = tm.dimension();
126 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
127 dim.des += TEXT_TO_INSET_OFFSET;
128 dim.asc += TEXT_TO_INSET_OFFSET;
133 void InsetText::write(Buffer const & buf, ostream & os) const
136 text_.write(buf, os);
140 void InsetText::read(Buffer const & buf, Lexer & lex)
144 // delete the initial paragraph
145 Paragraph oldpar = *paragraphs().begin();
146 paragraphs().clear();
148 bool res = text_.read(buf, lex, errorList);
152 lex.printError("Missing \\end_inset at this point. "
157 // ensure we have at least one paragraph.
158 if (paragraphs().empty())
159 paragraphs().push_back(oldpar);
163 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
165 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
167 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
169 // Hand font through to contained lyxtext:
170 tm.font_.fontInfo() = mi.base.font;
171 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
173 tm.metrics(mi, dim, mi.base.textwidth);
176 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
177 dim.asc += TEXT_TO_INSET_OFFSET;
178 dim.des += TEXT_TO_INSET_OFFSET;
179 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
183 void InsetText::draw(PainterInfo & pi, int x, int y) const
185 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
187 if (drawFrame_ || pi.full_repaint) {
188 int const w = tm.width() + 2 * TEXT_TO_INSET_OFFSET;
189 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
190 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
192 pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
194 pi.pain.rectangle(x, yframe, w, h, frameColor());
196 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
200 docstring const InsetText::editMessage() const
202 return _("Opened Text Inset");
206 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
208 pit_type const pit = front ? 0 : paragraphs().size() - 1;
209 pos_type pos = front ? 0 : paragraphs().back().size();
211 // if visual information is not to be ignored, move to extreme right/left
212 if (entry_from != ENTRY_DIRECTION_IGNORE) {
213 Cursor temp_cur = cur;
214 temp_cur.pit() = pit;
215 temp_cur.pos() = pos;
216 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
217 pos = temp_cur.pos();
220 text_.setCursor(cur.top(), pit, pos);
221 cur.clearSelection();
226 Inset * InsetText::editXY(Cursor & cur, int x, int y)
228 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
232 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
234 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
235 << " [ cmd.action = " << cmd.action << ']');
236 text_.dispatch(cur, cmd);
240 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
241 FuncStatus & status) const
243 return text_.getStatus(cur, cmd, status);
247 void InsetText::setChange(Change const & change)
249 ParagraphList::iterator pit = paragraphs().begin();
250 ParagraphList::iterator end = paragraphs().end();
251 for (; pit != end; ++pit) {
252 pit->setChange(change);
257 void InsetText::acceptChanges(BufferParams const & bparams)
259 text_.acceptChanges(bparams);
263 void InsetText::rejectChanges(BufferParams const & bparams)
265 text_.rejectChanges(bparams);
269 int InsetText::latex(Buffer const & buf, odocstream & os,
270 OutputParams const & runparams) const
273 latexParagraphs(buf, text_, os, texrow, runparams);
274 return texrow.rows();
278 int InsetText::plaintext(Buffer const & buf, odocstream & os,
279 OutputParams const & runparams) const
281 ParagraphList::const_iterator beg = paragraphs().begin();
282 ParagraphList::const_iterator end = paragraphs().end();
283 ParagraphList::const_iterator it = beg;
284 bool ref_printed = false;
286 for (; it != end; ++it) {
289 if (runparams.linelen > 0)
292 odocstringstream oss;
293 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
294 docstring const str = oss.str();
296 // FIXME: len is not computed fully correctly; in principle,
297 // we have to count the characters after the last '\n'
305 int InsetText::docbook(Buffer const & buf, odocstream & os,
306 OutputParams const & runparams) const
308 docbookParagraphs(paragraphs(), buf, os, runparams);
313 void InsetText::validate(LaTeXFeatures & features) const
315 for_each(paragraphs().begin(), paragraphs().end(),
316 bind(&Paragraph::validate, _1, ref(features)));
320 void InsetText::cursorPos(BufferView const & bv,
321 CursorSlice const & sl, bool boundary, int & x, int & y) const
323 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
324 y = bv.textMetrics(&text_).cursorY(sl, boundary);
328 bool InsetText::showInsetDialog(BufferView *) const
334 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
337 Paragraph & first = paragraphs().front();
338 for (unsigned int i = 0; i < data.length(); ++i)
339 first.insertChar(i, data[i], font, trackChanges);
343 void InsetText::setAutoBreakRows(bool flag)
345 if (flag == text_.autoBreakRows_)
348 text_.autoBreakRows_ = flag;
352 // remove previously existing newlines
353 ParagraphList::iterator it = paragraphs().begin();
354 ParagraphList::iterator end = paragraphs().end();
355 for (; it != end; ++it)
356 for (int i = 0; i < it->size(); ++i)
357 if (it->isNewline(i))
358 // do not track the change, because the user
359 // is not allowed to revert/reject it
360 it->eraseChar(i, false);
364 void InsetText::setDrawFrame(bool flag)
370 ColorCode InsetText::frameColor() const
376 void InsetText::setFrameColor(ColorCode col)
382 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
384 // There is little we can do here to keep track of changes.
385 // As of 2006/10/20, appendParagraphs is used exclusively by
386 // LyXTabular::setMultiColumn. In this context, the paragraph break
387 // is lost irreversibly and the appended text doesn't really change
389 ParagraphList & pl = paragraphs();
391 ParagraphList::iterator pit = plist.begin();
392 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
394 mergeParagraph(buffer->params(), pl,
395 distance(pl.begin(), ins) - 1);
397 for_each(pit, plist.end(),
398 bind(&ParagraphList::push_back, ref(pl), _1));
402 void InsetText::addPreview(PreviewLoader & loader) const
404 ParagraphList::const_iterator pit = paragraphs().begin();
405 ParagraphList::const_iterator pend = paragraphs().end();
407 for (; pit != pend; ++pit) {
408 InsetList::const_iterator it = pit->insetList().begin();
409 InsetList::const_iterator end = pit->insetList().end();
410 for (; it != end; ++it)
411 it->inset->addPreview(loader);
416 //FIXME: instead of this hack, which only works by chance,
417 // cells should have their own insetcell type, which returns CELL_CODE!
418 bool InsetText::neverIndent(Buffer const & buffer) const
420 // this is only true for tabular cells
421 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
425 ParagraphList const & InsetText::paragraphs() const
427 return text_.paragraphs();
431 ParagraphList & InsetText::paragraphs()
433 return text_.paragraphs();
437 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
439 ParIterator it2 = it;
441 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
442 lyx::updateLabels(buf, it2);