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>
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 : UpdatableInset(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;
197 text_.draw(pi, x + border_, y);
200 drawFrame(pi.pain, x, y);
204 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
206 // repaint the background if needed
207 if (backgroundColor() != LColor::background)
208 clearInset(pi.pain, x, y);
209 text_.drawSelection(pi, x, y);
213 void InsetText::drawFrame(Painter & pain, int x, int y) const
215 int const w = text_.width() + border_;
216 int const a = text_.ascent() + border_;
217 int const h = a + text_.descent() + border_;
218 pain.rectangle(x, y - a, w, h, frameColor());
222 void InsetText::clearInset(Painter & pain, int x, int y) const
224 int const w = text_.width() + border_;
225 int const a = text_.ascent() + border_;
226 int const h = a + text_.descent() + border_;
227 pain.fillRectangle(x, y - a, w, h, backgroundColor());
231 void InsetText::updateLocal(LCursor & cur)
233 if (!text_.autoBreakRows_ && paragraphs().size() > 1) {
234 // collapse paragraphs
235 while (paragraphs().size() > 1) {
236 ParagraphList::iterator const first = paragraphs().begin();
237 ParagraphList::iterator second = first;
239 size_t const first_par_size = first->size();
241 if (!first->empty() &&
243 !first->isSeparator(first_par_size - 1)) {
244 first->insertChar(first_par_size, ' ');
247 cur.clearSelection();
248 mergeParagraph(cur.buffer().params(), paragraphs(), 0);
252 if (!cur.selection())
255 LyXView * lv = cur.bv().owner();
256 lv->view_state_changed();
258 lv->updateToolbars();
259 if (old_pit != cur.pit()) {
260 lv->setLayout(text_.getPar(cur.pit()).layout()->name());
266 string const InsetText::editMessage() const
268 return _("Opened Text Inset");
272 void InsetText::edit(LCursor & cur, bool left)
274 //lyxerr << "InsetText: edit left/right" << endl;
276 setViewCache(&cur.bv());
277 int const pit = left ? 0 : paragraphs().size() - 1;
278 int const pos = left ? 0 : paragraphs().back().size();
279 text_.setCursor(cur.top(), pit, pos);
280 cur.clearSelection();
283 #warning can someone check if/when this is needed?
290 InsetBase * InsetText::editXY(LCursor & cur, int x, int y) const
293 return text_.editXY(cur, x, y);
294 //sanitizeEmptyText(cur.bv());
299 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
301 lyxerr << BOOST_CURRENT_FUNCTION
302 << " [ cmd.action = " << cmd.action << ']' << endl;
303 setViewCache(&cur.bv());
305 text_.dispatch(cur, cmd);
310 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
311 FuncStatus & status) const
313 return text_.getStatus(cur, cmd, status);
317 int InsetText::latex(Buffer const & buf, ostream & os,
318 OutputParams const & runparams) const
321 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
322 return texrow.rows();
326 int InsetText::plaintext(Buffer const & buf, ostream & os,
327 OutputParams const & runparams) const
329 ParagraphList::const_iterator beg = paragraphs().begin();
330 ParagraphList::const_iterator end = paragraphs().end();
331 ParagraphList::const_iterator it = beg;
332 for (; it != end; ++it)
333 asciiParagraph(buf, *it, os, runparams, it == beg);
335 // FIXME: Give the total numbers of lines
340 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
341 OutputParams const & runparams) const
343 linuxdocParagraphs(buf, paragraphs(), os, runparams);
348 int InsetText::docbook(Buffer const & buf, ostream & os,
349 OutputParams const & runparams) const
351 docbookParagraphs(paragraphs(), buf, os, runparams);
356 void InsetText::validate(LaTeXFeatures & features) const
358 for_each(paragraphs().begin(), paragraphs().end(),
359 bind(&Paragraph::validate, _1, ref(features)));
363 void InsetText::getCursorPos(CursorSlice const & sl, int & x, int & y) const
365 x = text_.cursorX(sl) + border_;
366 y = text_.cursorY(sl);
370 bool InsetText::showInsetDialog(BufferView *) const
376 void InsetText::getLabelList(Buffer const & buffer,
377 std::vector<string> & list) const
379 ParagraphList::const_iterator pit = paragraphs().begin();
380 ParagraphList::const_iterator pend = paragraphs().end();
381 for (; pit != pend; ++pit) {
382 InsetList::const_iterator beg = pit->insetlist.begin();
383 InsetList::const_iterator end = pit->insetlist.end();
384 for (; beg != end; ++beg)
385 beg->inset->getLabelList(buffer, list);
390 void InsetText::markNew(bool track_changes)
392 ParagraphList::iterator pit = paragraphs().begin();
393 ParagraphList::iterator end = paragraphs().end();
394 for (; pit != end; ++pit) {
398 // no-op when not tracking
405 void InsetText::setText(string const & data, LyXFont const & font)
408 Paragraph & first = paragraphs().front();
409 for (unsigned int i = 0; i < data.length(); ++i)
410 first.insertChar(i, data[i], font);
414 void InsetText::setAutoBreakRows(bool flag)
416 if (flag != text_.autoBreakRows_) {
417 text_.autoBreakRows_ = flag;
424 void InsetText::setDrawFrame(bool flag)
430 LColor_color InsetText::frameColor() const
432 return LColor::color(frame_color_);
436 void InsetText::setFrameColor(LColor_color col)
442 void InsetText::setViewCache(BufferView const * bv) const
444 if (bv && bv != text_.bv_owner) {
445 //lyxerr << "setting view cache from "
446 // << text_.bv_owner << " to " << bv << "\n";
447 text_.bv_owner = const_cast<BufferView *>(bv);
452 void InsetText::removeNewlines()
454 ParagraphList::iterator it = paragraphs().begin();
455 ParagraphList::iterator end = paragraphs().end();
456 for (; it != end; ++it)
457 for (int i = 0; i < it->size(); ++i)
458 if (it->isNewline(i))
463 LyXText * InsetText::getText(int i) const
465 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
469 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
472 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
473 // And it probably does. You have to take a look at this John. (Lgb)
474 #warning John, have a look here. (Lgb)
476 ParagraphList & pl = paragraphs();
478 ParagraphList::iterator pit = plist.begin();
479 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
481 mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
483 for_each(pit, plist.end(),
484 bind(&ParagraphList::push_back, ref(pl), _1));
488 void InsetText::addPreview(PreviewLoader & loader) const
490 ParagraphList::const_iterator pit = paragraphs().begin();
491 ParagraphList::const_iterator pend = paragraphs().end();
493 for (; pit != pend; ++pit) {
494 InsetList::const_iterator it = pit->insetlist.begin();
495 InsetList::const_iterator end = pit->insetlist.end();
496 for (; it != end; ++it)
497 it->inset->addPreview(loader);
502 ParagraphList const & InsetText::paragraphs() const
504 return text_.paragraphs();
508 ParagraphList & InsetText::paragraphs()
510 return text_.paragraphs();