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());
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 LayoutPtr old_layout = pars.begin()->layout();
126 pars.push_back(Paragraph());
127 pars.begin()->setInsetOwner(this);
128 pars.begin()->layout(old_layout);
132 Inset * InsetText::clone() const
134 return 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 if (drawFrame_ || pi.full_repaint) {
196 int const w = hasFixedWidth() ?
197 tm.maxWidth() : tm.width() + 2 * border_;
198 int const a = tm.ascent() + border_;
199 int const h = a + tm.descent() + border_;
201 pi.pain.fillRectangle(x, y - a, w, h, backgroundColor());
203 pi.pain.rectangle(x, y - a, w, h, frameColor());
205 text_.drawSelection(pi, x + border_, y);
206 tm.draw(pi, x + border_, y);
210 docstring const InsetText::editMessage() const
212 return _("Opened Text Inset");
216 void InsetText::edit(Cursor & cur, bool left)
218 //lyxerr << "InsetText: edit left/right" << endl;
219 int const pit = left ? 0 : paragraphs().size() - 1;
220 int const pos = left ? 0 : paragraphs().back().size();
221 text_.setCursor(cur.top(), pit, pos);
222 cur.clearSelection();
227 Inset * InsetText::editXY(Cursor & cur, int x, int y)
229 return text_.editXY(cur, x, y);
233 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
235 LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
236 << " [ cmd.action = "
237 << cmd.action << ']' << endl;
238 text_.dispatch(cur, cmd);
242 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
243 FuncStatus & status) const
245 return text_.getStatus(cur, cmd, status);
249 void InsetText::setChange(Change const & change)
251 ParagraphList::iterator pit = paragraphs().begin();
252 ParagraphList::iterator end = paragraphs().end();
253 for (; pit != end; ++pit) {
254 pit->setChange(change);
259 void InsetText::acceptChanges(BufferParams const & bparams)
261 text_.acceptChanges(bparams);
265 void InsetText::rejectChanges(BufferParams const & bparams)
267 text_.rejectChanges(bparams);
271 int InsetText::latex(Buffer const & buf, odocstream & os,
272 OutputParams const & runparams) const
275 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
276 return texrow.rows();
280 int InsetText::plaintext(Buffer const & buf, odocstream & os,
281 OutputParams const & runparams) const
283 ParagraphList::const_iterator beg = paragraphs().begin();
284 ParagraphList::const_iterator end = paragraphs().end();
285 ParagraphList::const_iterator it = beg;
286 bool ref_printed = false;
288 for (; it != end; ++it) {
291 if (runparams.linelen > 0)
294 odocstringstream oss;
295 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
296 docstring const str = oss.str();
298 // FIXME: len is not computed fully correctly; in principle,
299 // we have to count the characters after the last '\n'
307 int InsetText::docbook(Buffer const & buf, odocstream & os,
308 OutputParams const & runparams) const
310 docbookParagraphs(paragraphs(), buf, os, runparams);
315 void InsetText::validate(LaTeXFeatures & features) const
317 for_each(paragraphs().begin(), paragraphs().end(),
318 bind(&Paragraph::validate, _1, ref(features)));
322 void InsetText::cursorPos(BufferView const & bv,
323 CursorSlice const & sl, bool boundary, int & x, int & y) const
325 x = text_.cursorX(bv, sl, boundary) + border_;
326 y = text_.cursorY(bv, sl, boundary);
330 bool InsetText::showInsetDialog(BufferView *) const
336 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
339 Paragraph & first = paragraphs().front();
340 for (unsigned int i = 0; i < data.length(); ++i)
341 first.insertChar(i, data[i], font, trackChanges);
345 void InsetText::setAutoBreakRows(bool flag)
347 if (flag == text_.autoBreakRows_)
350 text_.autoBreakRows_ = flag;
354 // remove previously existing newlines
355 ParagraphList::iterator it = paragraphs().begin();
356 ParagraphList::iterator end = paragraphs().end();
357 for (; it != end; ++it)
358 for (int i = 0; i < it->size(); ++i)
359 if (it->isNewline(i))
360 // do not track the change, because the user
361 // is not allowed to revert/reject it
362 it->eraseChar(i, false);
366 void InsetText::setDrawFrame(bool flag)
372 Color_color InsetText::frameColor() const
374 return Color::color(frame_color_);
378 void InsetText::setFrameColor(Color_color col)
384 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
386 // There is little we can do here to keep track of changes.
387 // As of 2006/10/20, appendParagraphs is used exclusively by
388 // LyXTabular::setMultiColumn. In this context, the paragraph break
389 // is lost irreversibly and the appended text doesn't really change
391 ParagraphList & pl = paragraphs();
393 ParagraphList::iterator pit = plist.begin();
394 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
396 mergeParagraph(buffer->params(), pl,
397 std::distance(pl.begin(), ins) - 1);
399 for_each(pit, plist.end(),
400 bind(&ParagraphList::push_back, ref(pl), _1));
404 void InsetText::addPreview(PreviewLoader & loader) const
406 ParagraphList::const_iterator pit = paragraphs().begin();
407 ParagraphList::const_iterator pend = paragraphs().end();
409 for (; pit != pend; ++pit) {
410 InsetList::const_iterator it = pit->insetlist.begin();
411 InsetList::const_iterator end = pit->insetlist.end();
412 for (; it != end; ++it)
413 it->inset->addPreview(loader);
418 //FIXME: instead of this hack, which only works by chance,
419 // cells should have their own insetcell type, which returns CELL_CODE!
420 bool InsetText::neverIndent(Buffer const & buffer) const
422 // this is only true for tabular cells
423 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
427 ParagraphList const & InsetText::paragraphs() const
429 return text_.paragraphs();
433 ParagraphList & InsetText::paragraphs()
435 return text_.paragraphs();
439 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
441 ParIterator it2 = it;
443 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
444 lyx::updateLabels(buf, it2);