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() + 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;
191 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
193 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
195 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
197 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
201 docstring const InsetText::editMessage() const
203 return _("Opened Text Inset");
207 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
209 pit_type const pit = front ? 0 : paragraphs().size() - 1;
210 pos_type pos = front ? 0 : paragraphs().back().size();
212 // if visual information is not to be ignored, move to extreme right/left
213 if (entry_from != ENTRY_DIRECTION_IGNORE) {
214 Cursor temp_cur = cur;
215 temp_cur.pit() = pit;
216 temp_cur.pos() = pos;
217 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
218 pos = temp_cur.pos();
221 text_.setCursor(cur.top(), pit, pos);
222 cur.clearSelection();
227 Inset * InsetText::editXY(Cursor & cur, int x, int y)
229 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
233 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
235 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
236 << " [ cmd.action = " << cmd.action << ']');
237 text_.dispatch(cur, cmd);
241 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
242 FuncStatus & status) const
244 return text_.getStatus(cur, cmd, status);
248 void InsetText::setChange(Change const & change)
250 ParagraphList::iterator pit = paragraphs().begin();
251 ParagraphList::iterator end = paragraphs().end();
252 for (; pit != end; ++pit) {
253 pit->setChange(change);
258 void InsetText::acceptChanges(BufferParams const & bparams)
260 text_.acceptChanges(bparams);
264 void InsetText::rejectChanges(BufferParams const & bparams)
266 text_.rejectChanges(bparams);
270 int InsetText::latex(Buffer const & buf, odocstream & os,
271 OutputParams const & runparams) const
274 latexParagraphs(buf, text_, os, texrow, runparams);
275 return texrow.rows();
279 int InsetText::plaintext(Buffer const & buf, odocstream & os,
280 OutputParams const & runparams) const
282 ParagraphList::const_iterator beg = paragraphs().begin();
283 ParagraphList::const_iterator end = paragraphs().end();
284 ParagraphList::const_iterator it = beg;
285 bool ref_printed = false;
287 for (; it != end; ++it) {
290 if (runparams.linelen > 0)
293 odocstringstream oss;
294 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
295 docstring const str = oss.str();
297 // FIXME: len is not computed fully correctly; in principle,
298 // we have to count the characters after the last '\n'
306 int InsetText::docbook(Buffer const & buf, odocstream & os,
307 OutputParams const & runparams) const
309 docbookParagraphs(paragraphs(), buf, os, runparams);
314 void InsetText::validate(LaTeXFeatures & features) const
316 for_each(paragraphs().begin(), paragraphs().end(),
317 bind(&Paragraph::validate, _1, ref(features)));
321 void InsetText::cursorPos(BufferView const & bv,
322 CursorSlice const & sl, bool boundary, int & x, int & y) const
324 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
325 y = bv.textMetrics(&text_).cursorY(sl, boundary);
329 bool InsetText::showInsetDialog(BufferView *) const
335 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
338 Paragraph & first = paragraphs().front();
339 for (unsigned int i = 0; i < data.length(); ++i)
340 first.insertChar(i, data[i], font, trackChanges);
344 void InsetText::setAutoBreakRows(bool flag)
346 if (flag == text_.autoBreakRows_)
349 text_.autoBreakRows_ = flag;
353 // remove previously existing newlines
354 ParagraphList::iterator it = paragraphs().begin();
355 ParagraphList::iterator end = paragraphs().end();
356 for (; it != end; ++it)
357 for (int i = 0; i < it->size(); ++i)
358 if (it->isNewline(i))
359 // do not track the change, because the user
360 // is not allowed to revert/reject it
361 it->eraseChar(i, false);
365 void InsetText::setDrawFrame(bool flag)
371 ColorCode InsetText::frameColor() const
377 void InsetText::setFrameColor(ColorCode col)
383 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
385 // There is little we can do here to keep track of changes.
386 // As of 2006/10/20, appendParagraphs is used exclusively by
387 // LyXTabular::setMultiColumn. In this context, the paragraph break
388 // is lost irreversibly and the appended text doesn't really change
390 ParagraphList & pl = paragraphs();
392 ParagraphList::iterator pit = plist.begin();
393 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
395 mergeParagraph(buffer->params(), pl,
396 distance(pl.begin(), ins) - 1);
398 for_each(pit, plist.end(),
399 bind(&ParagraphList::push_back, ref(pl), _1));
403 void InsetText::addPreview(PreviewLoader & loader) const
405 ParagraphList::const_iterator pit = paragraphs().begin();
406 ParagraphList::const_iterator pend = paragraphs().end();
408 for (; pit != pend; ++pit) {
409 InsetList::const_iterator it = pit->insetList().begin();
410 InsetList::const_iterator end = pit->insetList().end();
411 for (; it != end; ++it)
412 it->inset->addPreview(loader);
417 //FIXME: instead of this hack, which only works by chance,
418 // cells should have their own insetcell type, which returns CELL_CODE!
419 bool InsetText::neverIndent(Buffer const & buffer) const
421 // this is only true for tabular cells
422 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
426 ParagraphList const & InsetText::paragraphs() const
428 return text_.paragraphs();
432 ParagraphList & InsetText::paragraphs()
434 return text_.paragraphs();
438 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
440 ParIterator it2 = it;
442 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
443 lyx::updateLabels(buf, it2);