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 "CutAndPaste.h"
22 #include "dispatchresult.h"
23 #include "errorlist.h"
24 #include "funcrequest.h"
32 #include "metricsinfo.h"
33 #include "output_docbook.h"
34 #include "output_latex.h"
35 #include "output_linuxdoc.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/font_metrics.h"
48 #include "frontends/Painter.h"
50 #include "support/lyxalgo.h" // lyx::count
52 #include <boost/bind.hpp>
53 #include <boost/current_function.hpp>
57 using lyx::graphics::PreviewLoader;
59 using lyx::support::isStrUnsignedInt;
73 int InsetText::border_ = 2;
76 InsetText::InsetText(BufferParams const & bp)
77 : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
79 paragraphs().push_back(Paragraph());
80 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
81 if (bp.tracking_changes)
82 paragraphs().back().trackChanges();
87 InsetText::InsetText(InsetText const & in)
88 : InsetOld(in), text_(in.text_.bv_owner)
90 text_.autoBreakRows_ = in.text_.autoBreakRows_;
91 drawFrame_ = in.drawFrame_;
92 frame_color_ = in.frame_color_;
93 text_.paragraphs() = in.text_.paragraphs();
98 InsetText::InsetText()
103 void InsetText::init()
105 for_each(paragraphs().begin(), paragraphs().end(),
106 bind(&Paragraph::setInsetOwner, _1, this));
110 void InsetText::markErased(bool erased)
112 ParagraphList & pars = paragraphs();
113 for_each(pars.begin(), pars.end(),
114 bind(&Paragraph::markErased, _1, erased));
118 void InsetText::clear()
120 ParagraphList & pars = paragraphs();
122 // This is a gross hack...
123 LyXLayout_ptr old_layout = pars.begin()->layout();
126 pars.push_back(Paragraph());
127 pars.begin()->setInsetOwner(this);
128 pars.begin()->layout(old_layout);
132 auto_ptr<InsetBase> InsetText::doClone() const
134 return auto_ptr<InsetBase>(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, LyXLex & lex)
150 #warning John, look here. Doesnt make much sense.
152 if (buf.params().tracking_changes)
153 paragraphs().begin()->trackChanges();
155 // delete the initial paragraph
156 Paragraph oldpar = *paragraphs().begin();
157 paragraphs().clear();
158 bool res = text_.read(buf, lex);
162 lex.printError("Missing \\end_inset at this point. "
167 // ensure we have at least one paragraph.
168 if (paragraphs().empty())
169 paragraphs().push_back(oldpar);
173 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
175 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
176 setViewCache(mi.base.bv);
177 mi.base.textwidth -= 2 * border_;
178 font_ = mi.base.font;
179 text_.font_ = mi.base.font;
180 text_.metrics(mi, dim);
183 dim.wid += 2 * border_;
184 mi.base.textwidth += 2 * border_;
189 void InsetText::draw(PainterInfo & pi, int x, int y) const
191 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
192 // update our idea of where we are
193 setPosCache(pi, x, y);
195 text_.draw(pi, x + border_, y);
198 int const w = text_.width() + 2 * border_;
199 int const a = text_.ascent() + border_;
200 int const h = a + text_.descent() + border_;
201 int const ww = pi.base.bv->workWidth();
203 pi.pain.line(0, y - a, ww, y - a, frameColor());
204 pi.pain.line(0, y - a + h, ww, y - a + h, frameColor());
206 pi.pain.rectangle(x, y - a, w, h, frameColor());
212 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
214 if (backgroundColor() != LColor::background) {
215 // repaint the background if needed
216 int const w = text_.width() + 2 * border_;
217 int const a = text_.ascent() + border_;
218 int const h = a + text_.descent() + border_;
219 pi.pain.fillRectangle(x, y - a, w, h, backgroundColor());
221 text_.drawSelection(pi, x, y);
225 string 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 setViewCache(&cur.bv());
235 int const pit = left ? 0 : paragraphs().size() - 1;
236 int const pos = left ? 0 : paragraphs().back().size();
237 text_.setCursor(cur.top(), pit, pos);
238 cur.clearSelection();
243 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
245 return text_.editXY(cur, x, y);
249 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
251 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
252 << " [ cmd.action = " << 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 int InsetText::latex(Buffer const & buf, ostream & os,
266 OutputParams const & runparams) const
269 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
270 return texrow.rows();
274 int InsetText::plaintext(Buffer const & buf, ostream & os,
275 OutputParams const & runparams) const
277 ParagraphList::const_iterator beg = paragraphs().begin();
278 ParagraphList::const_iterator end = paragraphs().end();
279 ParagraphList::const_iterator it = beg;
280 bool ref_printed = false;
281 for (; it != end; ++it)
282 asciiParagraph(buf, *it, os, runparams, ref_printed);
284 // FIXME: Give the total numbers of lines
289 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
290 OutputParams const & runparams) const
292 linuxdocParagraphs(buf, paragraphs(), os, runparams);
297 int InsetText::docbook(Buffer const & buf, ostream & os,
298 OutputParams const & runparams) const
300 docbookParagraphs(paragraphs(), buf, os, runparams);
305 void InsetText::validate(LaTeXFeatures & features) const
307 for_each(paragraphs().begin(), paragraphs().end(),
308 bind(&Paragraph::validate, _1, ref(features)));
312 void InsetText::cursorPos
313 (CursorSlice const & sl, bool boundary, int & x, int & y) const
315 x = text_.cursorX(sl, boundary) + border_;
316 y = text_.cursorY(sl, boundary);
320 bool InsetText::showInsetDialog(BufferView *) const
326 void InsetText::getLabelList(Buffer const & buffer,
327 std::vector<string> & list) const
329 ParagraphList::const_iterator pit = paragraphs().begin();
330 ParagraphList::const_iterator pend = paragraphs().end();
331 for (; pit != pend; ++pit) {
332 InsetList::const_iterator beg = pit->insetlist.begin();
333 InsetList::const_iterator end = pit->insetlist.end();
334 for (; beg != end; ++beg)
335 beg->inset->getLabelList(buffer, list);
340 void InsetText::markNew(bool track_changes)
342 ParagraphList::iterator pit = paragraphs().begin();
343 ParagraphList::iterator end = paragraphs().end();
344 for (; pit != end; ++pit) {
347 else // no-op when not tracking
353 void InsetText::setText(string const & data, LyXFont const & font)
356 Paragraph & first = paragraphs().front();
357 for (unsigned int i = 0; i < data.length(); ++i)
358 first.insertChar(i, data[i], font);
362 void InsetText::setAutoBreakRows(bool flag)
364 if (flag == text_.autoBreakRows_)
367 text_.autoBreakRows_ = flag;
371 // remove previously existing newlines
372 ParagraphList::iterator it = paragraphs().begin();
373 ParagraphList::iterator end = paragraphs().end();
374 for (; it != end; ++it)
375 for (int i = 0; i < it->size(); ++i)
376 if (it->isNewline(i))
381 void InsetText::setDrawFrame(bool flag)
387 LColor_color InsetText::frameColor() const
389 return LColor::color(frame_color_);
393 void InsetText::setFrameColor(LColor_color col)
399 void InsetText::setViewCache(BufferView const * bv) const
401 if (bv && bv != text_.bv_owner) {
402 //lyxerr << "setting view cache from "
403 // << text_.bv_owner << " to " << bv << "\n";
404 text_.bv_owner = const_cast<BufferView *>(bv);
409 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
412 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
413 // And it probably does. You have to take a look at this John. (Lgb)
414 #warning John, have a look here. (Lgb)
416 ParagraphList & pl = paragraphs();
418 ParagraphList::iterator pit = plist.begin();
419 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
421 mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
423 for_each(pit, plist.end(),
424 bind(&ParagraphList::push_back, ref(pl), _1));
428 void InsetText::addPreview(PreviewLoader & loader) const
430 ParagraphList::const_iterator pit = paragraphs().begin();
431 ParagraphList::const_iterator pend = paragraphs().end();
433 for (; pit != pend; ++pit) {
434 InsetList::const_iterator it = pit->insetlist.begin();
435 InsetList::const_iterator end = pit->insetlist.end();
436 for (; it != end; ++it)
437 it->inset->addPreview(loader);
442 //FIXME: instead of this hack, which only works by chance,
443 // cells should have their own insetcell type, which returns CELL_CODE!
444 bool InsetText::neverIndent() const
446 // this is only true for tabular cells
447 return !text_.isMainText() && lyxCode() == TEXT_CODE;
451 ParagraphList const & InsetText::paragraphs() const
453 return text_.paragraphs();
457 ParagraphList & InsetText::paragraphs()
459 return text_.paragraphs();