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/LyXView.h"
49 #include "frontends/Painter.h"
51 #include "support/lyxalgo.h" // lyx::count
53 #include <boost/bind.hpp>
54 #include <boost/current_function.hpp>
58 using lyx::graphics::PreviewLoader;
60 using lyx::support::isStrUnsignedInt;
74 int InsetText::border_ = 2;
77 InsetText::InsetText(BufferParams const & bp)
78 : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
80 paragraphs().push_back(Paragraph());
81 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
82 if (bp.tracking_changes)
83 paragraphs().back().trackChanges();
88 InsetText::InsetText(InsetText const & in)
89 : UpdatableInset(in), text_(in.text_.bv_owner)
91 text_.autoBreakRows_ = in.text_.autoBreakRows_;
92 drawFrame_ = in.drawFrame_;
93 frame_color_ = in.frame_color_;
94 text_.paragraphs() = in.text_.paragraphs();
99 InsetText::InsetText()
104 void InsetText::init()
106 for_each(paragraphs().begin(), paragraphs().end(),
107 bind(&Paragraph::setInsetOwner, _1, this));
112 void InsetText::clear(bool just_mark_erased)
114 ParagraphList & pars = paragraphs();
115 if (just_mark_erased) {
116 for_each(pars.begin(), pars.end(),
117 bind(&Paragraph::markErased, _1));
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)
149 #warning John, look here. Doesnt make much sense.
151 if (buf.params().tracking_changes)
152 paragraphs().begin()->trackChanges();
154 // delete the initial paragraph
155 Paragraph oldpar = *paragraphs().begin();
156 paragraphs().clear();
157 bool res = text_.read(buf, lex);
161 lex.printError("Missing \\end_inset at this point. "
166 // ensure we have at least one paragraph.
167 if (paragraphs().empty())
168 paragraphs().push_back(oldpar);
172 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
174 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
175 setViewCache(mi.base.bv);
176 mi.base.textwidth -= 2 * border_;
177 font_ = mi.base.font;
178 text_.font_ = mi.base.font;
179 text_.metrics(mi, dim);
182 dim.wid += 2 * border_;
183 mi.base.textwidth += 2 * border_;
188 void InsetText::draw(PainterInfo & pi, int x, int y) const
190 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
191 // update our idea of where we are
192 setPosCache(pi, x, y);
194 BufferView * bv = pi.base.bv;
198 text_.draw(pi, x + border_, y);
201 drawFrame(pi.pain, x, y);
205 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
207 // repaint the background if needed
208 if (backgroundColor() != LColor::background)
209 clearInset(pi.pain, x, y);
210 text_.drawSelection(pi, x, y);
214 void InsetText::drawFrame(Painter & pain, int x, int y) const
216 int const w = text_.width() + 2 * border_;
217 int const a = text_.ascent() + border_;
218 int const h = a + text_.descent() + border_;
219 pain.rectangle(x, y - a, w, h, frameColor());
223 void InsetText::clearInset(Painter & pain, int x, int y) const
225 int const w = text_.width() + 2 * border_;
226 int const a = text_.ascent() + border_;
227 int const h = a + text_.descent() + border_;
228 pain.fillRectangle(x, y - a, w, h, backgroundColor());
232 void InsetText::updateLocal(LCursor & cur)
234 if (!text_.autoBreakRows_ && paragraphs().size() > 1) {
235 // collapse paragraphs
236 while (paragraphs().size() > 1) {
237 ParagraphList::iterator const first = paragraphs().begin();
238 ParagraphList::iterator second = first;
240 size_t const first_par_size = first->size();
242 if (!first->empty() &&
244 !first->isSeparator(first_par_size - 1)) {
245 first->insertChar(first_par_size, ' ');
248 cur.clearSelection();
249 mergeParagraph(cur.buffer().params(), paragraphs(), 0);
253 if (!cur.selection())
256 LyXView * lv = cur.bv().owner();
257 lv->view_state_changed();
259 lv->updateToolbars();
260 if (old_pit != cur.pit()) {
261 lv->setLayout(text_.getPar(cur.pit()).layout()->name());
267 string const InsetText::editMessage() const
269 return _("Opened Text Inset");
273 void InsetText::edit(LCursor & cur, bool left)
275 //lyxerr << "InsetText: edit left/right" << endl;
277 setViewCache(&cur.bv());
278 int const pit = left ? 0 : paragraphs().size() - 1;
279 int const pos = left ? 0 : paragraphs().back().size();
280 text_.setCursor(cur.top(), pit, pos);
281 cur.clearSelection();
284 #warning can someone check if/when this is needed?
291 InsetBase * InsetText::editXY(LCursor & cur, int x, int y) const
294 return text_.editXY(cur, x, y);
295 //sanitizeEmptyText(cur.bv());
300 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
302 lyxerr << BOOST_CURRENT_FUNCTION
303 << " [ cmd.action = " << cmd.action << ']' << endl;
304 setViewCache(&cur.bv());
306 text_.dispatch(cur, cmd);
311 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
312 FuncStatus & status) const
314 return text_.getStatus(cur, cmd, status);
318 int InsetText::latex(Buffer const & buf, ostream & os,
319 OutputParams const & runparams) const
322 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
323 return texrow.rows();
327 int InsetText::plaintext(Buffer const & buf, ostream & os,
328 OutputParams const & runparams) const
330 ParagraphList::const_iterator beg = paragraphs().begin();
331 ParagraphList::const_iterator end = paragraphs().end();
332 ParagraphList::const_iterator it = beg;
333 for (; it != end; ++it)
334 asciiParagraph(buf, *it, os, runparams, it == beg);
336 // FIXME: Give the total numbers of lines
341 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
342 OutputParams const & runparams) const
344 linuxdocParagraphs(buf, paragraphs(), os, runparams);
349 int InsetText::docbook(Buffer const & buf, ostream & os,
350 OutputParams const & runparams) const
352 docbookParagraphs(paragraphs(), buf, os, runparams);
357 void InsetText::validate(LaTeXFeatures & features) const
359 for_each(paragraphs().begin(), paragraphs().end(),
360 bind(&Paragraph::validate, _1, ref(features)));
364 void InsetText::getCursorPos(CursorSlice const & sl, int & x, int & y) const
366 x = text_.cursorX(sl) + border_;
367 y = text_.cursorY(sl);
371 bool InsetText::showInsetDialog(BufferView *) const
377 void InsetText::getLabelList(Buffer const & buffer,
378 std::vector<string> & list) const
380 ParagraphList::const_iterator pit = paragraphs().begin();
381 ParagraphList::const_iterator pend = paragraphs().end();
382 for (; pit != pend; ++pit) {
383 InsetList::const_iterator beg = pit->insetlist.begin();
384 InsetList::const_iterator end = pit->insetlist.end();
385 for (; beg != end; ++beg)
386 beg->inset->getLabelList(buffer, list);
391 void InsetText::markNew(bool track_changes)
393 ParagraphList::iterator pit = paragraphs().begin();
394 ParagraphList::iterator end = paragraphs().end();
395 for (; pit != end; ++pit) {
399 // no-op when not tracking
406 void InsetText::setText(string const & data, LyXFont const & font)
409 Paragraph & first = paragraphs().front();
410 for (unsigned int i = 0; i < data.length(); ++i)
411 first.insertChar(i, data[i], font);
415 void InsetText::setAutoBreakRows(bool flag)
417 if (flag != text_.autoBreakRows_) {
418 text_.autoBreakRows_ = flag;
425 void InsetText::setDrawFrame(bool flag)
431 LColor_color InsetText::frameColor() const
433 return LColor::color(frame_color_);
437 void InsetText::setFrameColor(LColor_color col)
443 void InsetText::setViewCache(BufferView const * bv) const
445 if (bv && bv != text_.bv_owner) {
446 //lyxerr << "setting view cache from "
447 // << text_.bv_owner << " to " << bv << "\n";
448 text_.bv_owner = const_cast<BufferView *>(bv);
453 void InsetText::removeNewlines()
455 ParagraphList::iterator it = paragraphs().begin();
456 ParagraphList::iterator end = paragraphs().end();
457 for (; it != end; ++it)
458 for (int i = 0; i < it->size(); ++i)
459 if (it->isNewline(i))
464 LyXText * InsetText::getText(int i) const
466 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
470 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
473 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
474 // And it probably does. You have to take a look at this John. (Lgb)
475 #warning John, have a look here. (Lgb)
477 ParagraphList & pl = paragraphs();
479 ParagraphList::iterator pit = plist.begin();
480 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
482 mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
484 for_each(pit, plist.end(),
485 bind(&ParagraphList::push_back, ref(pl), _1));
489 void InsetText::addPreview(PreviewLoader & loader) const
491 ParagraphList::const_iterator pit = paragraphs().begin();
492 ParagraphList::const_iterator pend = paragraphs().end();
494 for (; pit != pend; ++pit) {
495 InsetList::const_iterator it = pit->insetlist.begin();
496 InsetList::const_iterator end = pit->insetlist.end();
497 for (; it != end; ++it)
498 it->inset->addPreview(loader);
503 ParagraphList const & InsetText::paragraphs() const
505 return text_.paragraphs();
509 ParagraphList & InsetText::paragraphs()
511 return text_.paragraphs();