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_(in.text_.bv_owner)
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 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
169 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
170 setViewCache(mi.base.bv);
171 mi.base.textwidth -= 2 * border_;
172 font_ = mi.base.font;
173 // Hand font through to contained lyxtext:
174 text_.font_ = mi.base.font;
175 text_.metrics(mi, dim);
178 dim.wid += 2 * border_;
179 mi.base.textwidth += 2 * border_;
184 void InsetText::draw(PainterInfo & pi, int x, int y) const
186 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
187 // update our idea of where we are
188 setPosCache(pi, x, y);
190 text_.background_color_ = backgroundColor();
191 text_.draw(pi, x + border_, y);
194 int const w = text_.width() + 2 * border_;
195 int const a = text_.ascent() + border_;
196 int const h = a + text_.descent() + border_;
197 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
203 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
205 int const w = text_.width() + 2 * border_;
206 int const a = text_.ascent() + border_;
207 int const h = a + text_.descent() + border_;
208 pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
210 text_.drawSelection(pi, x, y);
214 bool InsetText::covers(BufferView & bv, int x, int y) const
216 return bv.coordCache().getInsets().has(this)
218 && x <= xo(bv) + width() + (Wide() ? text_.maxwidth_ : 0)
219 && y >= yo(bv) - ascent()
220 && y <= yo(bv) + descent();
224 docstring const InsetText::editMessage() const
226 return _("Opened Text Inset");
230 void InsetText::edit(LCursor & cur, bool left)
232 //lyxerr << "InsetText: edit left/right" << endl;
233 setViewCache(&cur.bv());
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 setViewCache(&cur.bv());
254 text_.dispatch(cur, cmd);
258 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
259 FuncStatus & status) const
261 return text_.getStatus(cur, cmd, status);
265 void InsetText::setChange(Change const & change)
267 ParagraphList::iterator pit = paragraphs().begin();
268 ParagraphList::iterator end = paragraphs().end();
269 for (; pit != end; ++pit) {
270 pit->setChange(change);
275 int InsetText::latex(Buffer const & buf, odocstream & os,
276 OutputParams const & runparams) const
279 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
280 return texrow.rows();
284 int InsetText::plaintext(Buffer const & buf, odocstream & os,
285 OutputParams const & runparams) const
287 ParagraphList::const_iterator beg = paragraphs().begin();
288 ParagraphList::const_iterator end = paragraphs().end();
289 ParagraphList::const_iterator it = beg;
290 bool ref_printed = false;
291 odocstringstream oss;
292 for (; it != end; ++it)
293 asciiParagraph(buf, *it, oss, runparams, ref_printed);
295 docstring const str = oss.str();
297 // Return how many newlines we issued.
298 return int(lyx::count(str.begin(), str.end(), '\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 = text_.cursorX(sl, boundary) + border_;
321 y = text_.cursorY(sl, boundary);
325 bool InsetText::showInsetDialog(BufferView *) const
331 void InsetText::setText(docstring const & data, LyXFont 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 LColor_color InsetText::frameColor() const
369 return LColor::color(frame_color_);
373 void InsetText::setFrameColor(LColor_color col)
379 void InsetText::setViewCache(BufferView const * bv) const
381 if (bv && bv != text_.bv_owner) {
382 //lyxerr << "setting view cache from "
383 // << text_.bv_owner << " to " << bv << "\n";
384 text_.bv_owner = const_cast<BufferView *>(bv);
389 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
391 // There is little we can do here to keep track of changes.
392 // As of 2006/10/20, appendParagraphs is used exclusively by
393 // LyXTabular::setMultiColumn. In this context, the paragraph break
394 // is lost irreversibly and the appended text doesn't really change
396 ParagraphList & pl = paragraphs();
398 ParagraphList::iterator pit = plist.begin();
399 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
401 mergeParagraph(buffer->params(), pl,
402 std::distance(pl.begin(), ins) - 1);
404 for_each(pit, plist.end(),
405 bind(&ParagraphList::push_back, ref(pl), _1));
409 void InsetText::addPreview(PreviewLoader & loader) const
411 ParagraphList::const_iterator pit = paragraphs().begin();
412 ParagraphList::const_iterator pend = paragraphs().end();
414 for (; pit != pend; ++pit) {
415 InsetList::const_iterator it = pit->insetlist.begin();
416 InsetList::const_iterator end = pit->insetlist.end();
417 for (; it != end; ++it)
418 it->inset->addPreview(loader);
423 //FIXME: instead of this hack, which only works by chance,
424 // cells should have their own insetcell type, which returns CELL_CODE!
425 bool InsetText::neverIndent() const
427 // this is only true for tabular cells
428 return !text_.isMainText() && lyxCode() == TEXT_CODE;
432 ParagraphList const & InsetText::paragraphs() const
434 return text_.paragraphs();
438 ParagraphList & InsetText::paragraphs()
440 return text_.paragraphs();