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));
111 void InsetText::markErased(bool erased)
113 ParagraphList & pars = paragraphs();
114 for_each(pars.begin(), pars.end(),
115 bind(&Paragraph::markErased, _1, erased));
119 void InsetText::clear()
121 ParagraphList & pars = paragraphs();
123 // This is a gross hack...
124 LyXLayout_ptr old_layout = pars.begin()->layout();
127 pars.push_back(Paragraph());
128 pars.begin()->setInsetOwner(this);
129 pars.begin()->layout(old_layout);
133 auto_ptr<InsetBase> InsetText::doClone() const
135 return auto_ptr<InsetBase>(new InsetText(*this));
139 void InsetText::write(Buffer const & buf, ostream & os) const
142 text_.write(buf, os);
146 void InsetText::read(Buffer const & buf, LyXLex & lex)
151 #warning John, look here. Doesnt make much sense.
153 if (buf.params().tracking_changes)
154 paragraphs().begin()->trackChanges();
156 // delete the initial paragraph
157 Paragraph oldpar = *paragraphs().begin();
158 paragraphs().clear();
159 bool res = text_.read(buf, lex);
163 lex.printError("Missing \\end_inset at this point. "
168 // ensure we have at least one paragraph.
169 if (paragraphs().empty())
170 paragraphs().push_back(oldpar);
174 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
176 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
177 setViewCache(mi.base.bv);
178 mi.base.textwidth -= 2 * border_;
179 font_ = mi.base.font;
180 text_.font_ = mi.base.font;
181 text_.metrics(mi, dim);
184 dim.wid += 2 * border_;
185 mi.base.textwidth += 2 * border_;
190 void InsetText::draw(PainterInfo & pi, int x, int y) const
192 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
193 // update our idea of where we are
194 setPosCache(pi, x, y);
196 text_.draw(pi, x + border_, y);
199 int const w = text_.width() + 2 * border_;
200 int const a = text_.ascent() + border_;
201 int const h = a + text_.descent() + border_;
202 int const ww = pi.base.bv->workWidth();
204 pi.pain.line(0, y - a, ww, y - a, frameColor());
205 pi.pain.line(0, y - a + h, ww, y - a + h, frameColor());
207 pi.pain.rectangle(x, y - a, w, h, frameColor());
213 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
215 if (backgroundColor() != LColor::background) {
216 // repaint the background if needed
217 int const w = text_.width() + 2 * border_;
218 int const a = text_.ascent() + border_;
219 int const h = a + text_.descent() + border_;
220 pi.pain.fillRectangle(x, y - a, w, h, backgroundColor());
222 text_.drawSelection(pi, x, y);
226 string const InsetText::editMessage() const
228 return _("Opened Text Inset");
232 void InsetText::edit(LCursor & cur, bool left)
234 //lyxerr << "InsetText: edit left/right" << endl;
236 setViewCache(&cur.bv());
237 int const pit = left ? 0 : paragraphs().size() - 1;
238 int const pos = left ? 0 : paragraphs().back().size();
239 text_.setCursor(cur.top(), pit, pos);
240 cur.clearSelection();
245 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
248 return text_.editXY(cur, x, y);
252 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
254 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
255 << " [ cmd.action = " << cmd.action << ']' << endl;
256 setViewCache(&cur.bv());
257 text_.dispatch(cur, cmd);
261 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
262 FuncStatus & status) const
264 return text_.getStatus(cur, cmd, status);
268 int InsetText::latex(Buffer const & buf, ostream & os,
269 OutputParams const & runparams) const
272 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
273 return texrow.rows();
277 int InsetText::plaintext(Buffer const & buf, ostream & os,
278 OutputParams const & runparams) const
280 ParagraphList::const_iterator beg = paragraphs().begin();
281 ParagraphList::const_iterator end = paragraphs().end();
282 ParagraphList::const_iterator it = beg;
283 bool ref_printed = false;
284 for (; it != end; ++it)
285 asciiParagraph(buf, *it, os, runparams, ref_printed);
287 // FIXME: Give the total numbers of lines
292 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
293 OutputParams const & runparams) const
295 linuxdocParagraphs(buf, paragraphs(), os, runparams);
300 int InsetText::docbook(Buffer const & buf, ostream & os,
301 OutputParams const & runparams) const
303 docbookParagraphs(paragraphs(), buf, os, runparams);
308 void InsetText::validate(LaTeXFeatures & features) const
310 for_each(paragraphs().begin(), paragraphs().end(),
311 bind(&Paragraph::validate, _1, ref(features)));
315 void InsetText::cursorPos
316 (CursorSlice const & sl, bool boundary, int & x, int & y) const
318 x = text_.cursorX(sl, boundary) + border_;
319 y = text_.cursorY(sl, boundary);
323 bool InsetText::showInsetDialog(BufferView *) const
329 void InsetText::getLabelList(Buffer const & buffer,
330 std::vector<string> & list) const
332 ParagraphList::const_iterator pit = paragraphs().begin();
333 ParagraphList::const_iterator pend = paragraphs().end();
334 for (; pit != pend; ++pit) {
335 InsetList::const_iterator beg = pit->insetlist.begin();
336 InsetList::const_iterator end = pit->insetlist.end();
337 for (; beg != end; ++beg)
338 beg->inset->getLabelList(buffer, list);
343 void InsetText::markNew(bool track_changes)
345 ParagraphList::iterator pit = paragraphs().begin();
346 ParagraphList::iterator end = paragraphs().end();
347 for (; pit != end; ++pit) {
350 else // no-op when not tracking
356 void InsetText::setText(string const & data, LyXFont const & font)
359 Paragraph & first = paragraphs().front();
360 for (unsigned int i = 0; i < data.length(); ++i)
361 first.insertChar(i, data[i], font);
365 void InsetText::setAutoBreakRows(bool flag)
367 if (flag == text_.autoBreakRows_)
370 text_.autoBreakRows_ = flag;
374 // remove previously existing newlines
375 ParagraphList::iterator it = paragraphs().begin();
376 ParagraphList::iterator end = paragraphs().end();
377 for (; it != end; ++it)
378 for (int i = 0; i < it->size(); ++i)
379 if (it->isNewline(i))
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::setViewCache(BufferView const * bv) const
404 if (bv && bv != text_.bv_owner) {
405 //lyxerr << "setting view cache from "
406 // << text_.bv_owner << " to " << bv << "\n";
407 text_.bv_owner = const_cast<BufferView *>(bv);
412 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
415 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
416 // And it probably does. You have to take a look at this John. (Lgb)
417 #warning John, have a look here. (Lgb)
419 ParagraphList & pl = paragraphs();
421 ParagraphList::iterator pit = plist.begin();
422 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
424 mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
426 for_each(pit, plist.end(),
427 bind(&ParagraphList::push_back, ref(pl), _1));
431 void InsetText::addPreview(PreviewLoader & loader) const
433 ParagraphList::const_iterator pit = paragraphs().begin();
434 ParagraphList::const_iterator pend = paragraphs().end();
436 for (; pit != pend; ++pit) {
437 InsetList::const_iterator it = pit->insetlist.begin();
438 InsetList::const_iterator end = pit->insetlist.end();
439 for (; it != end; ++it)
440 it->inset->addPreview(loader);
445 //FIXME: instead of this hack, which only works by chance,
446 // cells should have their own insetcell type, which returns CELL_CODE!
447 bool InsetText::neverIndent() const
449 // this is only true for tabular cells
450 return !text_.isMainText() && lyxCode() == TEXT_CODE;
454 ParagraphList const & InsetText::paragraphs() const
456 return text_.paragraphs();
460 ParagraphList & InsetText::paragraphs()
462 return text_.paragraphs();