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 "bufferparams.h"
18 #include "BufferView.h"
19 #include "coordcache.h"
20 #include "CutAndPaste.h"
23 #include "dispatchresult.h"
24 #include "errorlist.h"
25 #include "funcrequest.h"
33 #include "metricsinfo.h"
34 #include "output_docbook.h"
35 #include "output_latex.h"
36 #include "output_plaintext.h"
37 #include "paragraph.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
40 #include "rowpainter.h"
46 #include "frontends/Alert.h"
47 #include "frontends/Painter.h"
49 #include "support/lyxalgo.h" // count
51 #include <boost/bind.hpp>
52 #include <boost/current_function.hpp>
59 using graphics::PreviewLoader;
61 using support::isStrUnsignedInt;
75 int InsetText::border_ = 2;
78 InsetText::InsetText(BufferParams const & bp)
79 : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
81 paragraphs().push_back(Paragraph());
82 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
83 // Dispose of the infamous L-shaped cursor.
84 text_.current_font.setLanguage(bp.language);
85 text_.real_current_font.setLanguage(bp.language);
90 InsetText::InsetText(InsetText const & in)
91 : InsetOld(in), text_()
93 text_.autoBreakRows_ = in.text_.autoBreakRows_;
94 drawFrame_ = in.drawFrame_;
95 frame_color_ = in.frame_color_;
96 text_.paragraphs() = in.text_.paragraphs();
97 // Hand current buffer language down to "cloned" textinsets
99 text_.current_font = in.text_.current_font;
100 text_.real_current_font = in.text_.real_current_font;
105 InsetText::InsetText()
110 void InsetText::init()
112 for_each(paragraphs().begin(), paragraphs().end(),
113 bind(&Paragraph::setInsetOwner, _1, this));
117 void InsetText::clear()
119 ParagraphList & pars = paragraphs();
121 // This is a gross hack...
122 LyXLayout_ptr old_layout = pars.begin()->layout();
125 pars.push_back(Paragraph());
126 pars.begin()->setInsetOwner(this);
127 pars.begin()->layout(old_layout);
131 auto_ptr<InsetBase> InsetText::doClone() const
133 return auto_ptr<InsetBase>(new InsetText(*this));
137 void InsetText::write(Buffer const & buf, ostream & os) const
140 text_.write(buf, os);
144 void InsetText::read(Buffer const & buf, LyXLex & lex)
148 // delete the initial paragraph
149 Paragraph oldpar = *paragraphs().begin();
150 paragraphs().clear();
152 bool res = text_.read(buf, lex, errorList);
156 lex.printError("Missing \\end_inset at this point. "
161 // ensure we have at least one paragraph.
162 if (paragraphs().empty())
163 paragraphs().push_back(oldpar);
167 bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
169 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
170 mi.base.textwidth -= 2 * border_;
171 font_ = mi.base.font;
172 // Hand font through to contained lyxtext:
173 text_.font_ = mi.base.font;
174 text_.metrics(mi, dim);
177 dim.wid += 2 * border_;
178 mi.base.textwidth += 2 * border_;
179 bool const changed = dim_ != dim;
185 void InsetText::draw(PainterInfo & pi, int x, int y) const
187 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
188 // update our idea of where we are
189 setPosCache(pi, x, y);
191 text_.background_color_ = backgroundColor();
192 text_.draw(pi, x + border_, y);
195 int const w = text_.width() + 2 * border_;
196 int const a = text_.ascent() + border_;
197 int const h = a + text_.descent() + border_;
198 pi.pain.rectangle(x, y - a, (wide() ? text_.maxwidth_ : w), h,
204 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
206 int const w = text_.width() + 2 * border_;
207 int const a = text_.ascent() + border_;
208 int const h = a + text_.descent() + border_;
209 pi.pain.fillRectangle(x, y - a, (wide() ? text_.maxwidth_ : w), h,
211 text_.drawSelection(pi, x, y);
215 bool InsetText::covers(BufferView const & bv, int x, int y) const
217 return bv.coordCache().getInsets().has(this)
219 && x <= xo(bv) + width() + (wide() ? text_.maxwidth_ : 0)
220 && y >= yo(bv) - ascent()
221 && y <= yo(bv) + descent();
225 docstring const InsetText::editMessage() const
227 return _("Opened Text Inset");
231 void InsetText::edit(LCursor & cur, bool left)
233 //lyxerr << "InsetText: edit left/right" << endl;
234 int const pit = left ? 0 : paragraphs().size() - 1;
235 int const pos = left ? 0 : paragraphs().back().size();
236 text_.setCursor(cur.top(), pit, pos);
237 cur.clearSelection();
242 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
244 return text_.editXY(cur, x, y);
248 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
250 lyxerr[Debug::ACTION] << BOOST_CURRENT_FUNCTION
251 << " [ cmd.action = "
252 << cmd.action << ']' << endl;
253 text_.dispatch(cur, cmd);
257 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
258 FuncStatus & status) const
260 return text_.getStatus(cur, cmd, status);
264 void InsetText::setChange(Change const & change)
266 ParagraphList::iterator pit = paragraphs().begin();
267 ParagraphList::iterator end = paragraphs().end();
268 for (; pit != end; ++pit) {
269 pit->setChange(change);
274 void InsetText::acceptChanges()
276 ParagraphList::iterator pit = paragraphs().begin();
277 ParagraphList::iterator end = paragraphs().end();
278 for (; pit != end; ++pit) {
279 // FIXME: change tracking (MG)
280 // we must handle end-of-par chars!
281 pit->acceptChanges(0, pit->size() + 1);
286 void InsetText::rejectChanges()
288 ParagraphList::iterator pit = paragraphs().begin();
289 ParagraphList::iterator end = paragraphs().end();
290 for (; pit != end; ++pit) {
291 // FIXME: change tracking (MG)
292 // we must handle end-of-par chars!
293 pit->rejectChanges(0, pit->size() + 1);
298 int InsetText::latex(Buffer const & buf, odocstream & os,
299 OutputParams const & runparams) const
302 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
303 return texrow.rows();
307 int InsetText::plaintext(Buffer const & buf, odocstream & os,
308 OutputParams const & runparams) const
310 ParagraphList::const_iterator beg = paragraphs().begin();
311 ParagraphList::const_iterator end = paragraphs().end();
312 ParagraphList::const_iterator it = beg;
313 bool ref_printed = false;
314 odocstringstream oss;
315 for (; it != end; ++it)
316 asciiParagraph(buf, *it, oss, runparams, ref_printed);
318 docstring const str = oss.str();
320 // Return how many newlines we issued.
321 return int(lyx::count(str.begin(), str.end(), '\n'));
325 int InsetText::docbook(Buffer const & buf, odocstream & os,
326 OutputParams const & runparams) const
328 docbookParagraphs(paragraphs(), buf, os, runparams);
333 void InsetText::validate(LaTeXFeatures & features) const
335 for_each(paragraphs().begin(), paragraphs().end(),
336 bind(&Paragraph::validate, _1, ref(features)));
340 void InsetText::cursorPos(BufferView const & bv,
341 CursorSlice const & sl, bool boundary, int & x, int & y) const
343 x = text_.cursorX(*bv.buffer(), sl, boundary) + border_;
344 y = text_.cursorY(sl, boundary);
348 bool InsetText::showInsetDialog(BufferView *) const
354 void InsetText::setText(docstring const & data, LyXFont const & font, bool trackChanges)
357 Paragraph & first = paragraphs().front();
358 for (unsigned int i = 0; i < data.length(); ++i)
359 first.insertChar(i, data[i], font, trackChanges);
363 void InsetText::setAutoBreakRows(bool flag)
365 if (flag == text_.autoBreakRows_)
368 text_.autoBreakRows_ = flag;
372 // remove previously existing newlines
373 ParagraphList::iterator it = paragraphs().begin();
374 ParagraphList::iterator end = paragraphs().end();
375 for (; it != end; ++it)
376 for (int i = 0; i < it->size(); ++i)
377 if (it->isNewline(i))
378 // do not track the change, because the user
379 // is not allowed to revert/reject it
380 it->eraseChar(i, false);
384 void InsetText::setDrawFrame(bool flag)
390 LColor_color InsetText::frameColor() const
392 return LColor::color(frame_color_);
396 void InsetText::setFrameColor(LColor_color col)
402 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
404 // There is little we can do here to keep track of changes.
405 // As of 2006/10/20, appendParagraphs is used exclusively by
406 // LyXTabular::setMultiColumn. In this context, the paragraph break
407 // is lost irreversibly and the appended text doesn't really change
409 ParagraphList & pl = paragraphs();
411 ParagraphList::iterator pit = plist.begin();
412 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
414 mergeParagraph(buffer->params(), pl,
415 std::distance(pl.begin(), ins) - 1);
417 for_each(pit, plist.end(),
418 bind(&ParagraphList::push_back, ref(pl), _1));
422 void InsetText::addPreview(PreviewLoader & loader) const
424 ParagraphList::const_iterator pit = paragraphs().begin();
425 ParagraphList::const_iterator pend = paragraphs().end();
427 for (; pit != pend; ++pit) {
428 InsetList::const_iterator it = pit->insetlist.begin();
429 InsetList::const_iterator end = pit->insetlist.end();
430 for (; it != end; ++it)
431 it->inset->addPreview(loader);
436 //FIXME: instead of this hack, which only works by chance,
437 // cells should have their own insetcell type, which returns CELL_CODE!
438 bool InsetText::neverIndent(Buffer const & buffer) const
440 // this is only true for tabular cells
441 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
445 ParagraphList const & InsetText::paragraphs() const
447 return text_.paragraphs();
451 ParagraphList & InsetText::paragraphs()
453 return text_.paragraphs();