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::clear(bool just_mark_erased)
113 ParagraphList & pars = paragraphs();
114 if (just_mark_erased) {
115 for_each(pars.begin(), pars.end(),
116 bind(&Paragraph::markErased, _1));
120 // This is a gross hack...
121 LyXLayout_ptr old_layout = pars.begin()->layout();
124 pars.push_back(Paragraph());
125 pars.begin()->setInsetOwner(this);
126 pars.begin()->layout(old_layout);
130 auto_ptr<InsetBase> InsetText::doClone() const
132 return auto_ptr<InsetBase>(new InsetText(*this));
136 void InsetText::write(Buffer const & buf, ostream & os) const
139 text_.write(buf, os);
143 void InsetText::read(Buffer const & buf, LyXLex & lex)
148 #warning John, look here. Doesnt make much sense.
150 if (buf.params().tracking_changes)
151 paragraphs().begin()->trackChanges();
153 // delete the initial paragraph
154 Paragraph oldpar = *paragraphs().begin();
155 paragraphs().clear();
156 bool res = text_.read(buf, lex);
160 lex.printError("Missing \\end_inset at this point. "
165 // ensure we have at least one paragraph.
166 if (paragraphs().empty())
167 paragraphs().push_back(oldpar);
171 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
173 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
174 setViewCache(mi.base.bv);
175 mi.base.textwidth -= 2 * border_;
176 font_ = mi.base.font;
177 text_.font_ = mi.base.font;
178 text_.metrics(mi, dim);
181 dim.wid += 2 * border_;
182 mi.base.textwidth += 2 * border_;
187 void InsetText::draw(PainterInfo & pi, int x, int y) const
189 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
190 // update our idea of where we are
191 setPosCache(pi, x, y);
193 BufferView * bv = pi.base.bv;
196 text_.draw(pi, x + border_, y);
199 drawFrame(pi.pain, x, y);
203 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
205 // repaint the background if needed
206 if (backgroundColor() != LColor::background)
207 clearInset(pi.pain, x, y);
208 text_.drawSelection(pi, x, y);
212 void InsetText::drawFrame(Painter & pain, int x, int y) const
214 int const w = text_.width() + 2 * border_;
215 int const a = text_.ascent() + border_;
216 int const h = a + text_.descent() + border_;
217 pain.rectangle(x, y - a, w, h, frameColor());
221 void InsetText::clearInset(Painter & pain, int x, int y) const
223 int const w = text_.width() + 2 * border_;
224 int const a = text_.ascent() + border_;
225 int const h = a + text_.descent() + border_;
226 pain.fillRectangle(x, y - a, w, h, backgroundColor());
230 string const InsetText::editMessage() const
232 return _("Opened Text Inset");
236 void InsetText::edit(LCursor & cur, bool left)
238 //lyxerr << "InsetText: edit left/right" << endl;
240 setViewCache(&cur.bv());
241 int const pit = left ? 0 : paragraphs().size() - 1;
242 int const pos = left ? 0 : paragraphs().back().size();
243 text_.setCursor(cur.top(), pit, pos);
244 cur.clearSelection();
249 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
252 return text_.editXY(cur, x, y);
256 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
258 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
259 << " [ cmd.action = " << cmd.action << ']' << endl;
260 setViewCache(&cur.bv());
261 text_.dispatch(cur, cmd);
265 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
266 FuncStatus & status) const
268 return text_.getStatus(cur, cmd, status);
272 int InsetText::latex(Buffer const & buf, ostream & os,
273 OutputParams const & runparams) const
276 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
277 return texrow.rows();
281 int InsetText::plaintext(Buffer const & buf, ostream & os,
282 OutputParams const & runparams) const
284 ParagraphList::const_iterator beg = paragraphs().begin();
285 ParagraphList::const_iterator end = paragraphs().end();
286 ParagraphList::const_iterator it = beg;
287 bool ref_printed = false;
288 for (; it != end; ++it)
289 asciiParagraph(buf, *it, os, runparams, ref_printed);
291 // FIXME: Give the total numbers of lines
296 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
297 OutputParams const & runparams) const
299 linuxdocParagraphs(buf, paragraphs(), os, runparams);
304 int InsetText::docbook(Buffer const & buf, ostream & os,
305 OutputParams const & runparams) const
307 docbookParagraphs(paragraphs(), buf, os, runparams);
312 void InsetText::validate(LaTeXFeatures & features) const
314 for_each(paragraphs().begin(), paragraphs().end(),
315 bind(&Paragraph::validate, _1, ref(features)));
319 void InsetText::cursorPos
320 (CursorSlice const & sl, bool boundary, int & x, int & y) const
322 x = text_.cursorX(sl, boundary) + border_;
323 y = text_.cursorY(sl, boundary);
327 bool InsetText::showInsetDialog(BufferView *) const
333 void InsetText::getLabelList(Buffer const & buffer,
334 std::vector<string> & list) const
336 ParagraphList::const_iterator pit = paragraphs().begin();
337 ParagraphList::const_iterator pend = paragraphs().end();
338 for (; pit != pend; ++pit) {
339 InsetList::const_iterator beg = pit->insetlist.begin();
340 InsetList::const_iterator end = pit->insetlist.end();
341 for (; beg != end; ++beg)
342 beg->inset->getLabelList(buffer, list);
347 void InsetText::markNew(bool track_changes)
349 ParagraphList::iterator pit = paragraphs().begin();
350 ParagraphList::iterator end = paragraphs().end();
351 for (; pit != end; ++pit) {
354 else // no-op when not tracking
360 void InsetText::setText(string const & data, LyXFont const & font)
363 Paragraph & first = paragraphs().front();
364 for (unsigned int i = 0; i < data.length(); ++i)
365 first.insertChar(i, data[i], font);
369 void InsetText::setAutoBreakRows(bool flag)
371 if (flag != text_.autoBreakRows_) {
372 text_.autoBreakRows_ = flag;
379 void InsetText::setDrawFrame(bool flag)
385 LColor_color InsetText::frameColor() const
387 return LColor::color(frame_color_);
391 void InsetText::setFrameColor(LColor_color col)
397 void InsetText::setViewCache(BufferView const * bv) const
399 if (bv && bv != text_.bv_owner) {
400 //lyxerr << "setting view cache from "
401 // << text_.bv_owner << " to " << bv << "\n";
402 text_.bv_owner = const_cast<BufferView *>(bv);
407 void InsetText::removeNewlines()
409 ParagraphList::iterator it = paragraphs().begin();
410 ParagraphList::iterator end = paragraphs().end();
411 for (; it != end; ++it)
412 for (int i = 0; i < it->size(); ++i)
413 if (it->isNewline(i))
418 LyXText * InsetText::getText(int i) const
420 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
424 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
427 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
428 // And it probably does. You have to take a look at this John. (Lgb)
429 #warning John, have a look here. (Lgb)
431 ParagraphList & pl = paragraphs();
433 ParagraphList::iterator pit = plist.begin();
434 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
436 mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
438 for_each(pit, plist.end(),
439 bind(&ParagraphList::push_back, ref(pl), _1));
443 void InsetText::addPreview(PreviewLoader & loader) const
445 ParagraphList::const_iterator pit = paragraphs().begin();
446 ParagraphList::const_iterator pend = paragraphs().end();
448 for (; pit != pend; ++pit) {
449 InsetList::const_iterator it = pit->insetlist.begin();
450 InsetList::const_iterator end = pit->insetlist.end();
451 for (; it != end; ++it)
452 it->inset->addPreview(loader);
457 ParagraphList const & InsetText::paragraphs() const
459 return text_.paragraphs();
463 ParagraphList & InsetText::paragraphs()
465 return text_.paragraphs();