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;
60 using lyx::support::strToUnsignedInt;
74 InsetText::InsetText(BufferParams const & bp)
75 : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
77 paragraphs().push_back(Paragraph());
78 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
79 if (bp.tracking_changes)
80 paragraphs().back().trackChanges();
85 InsetText::InsetText(InsetText const & in)
86 : UpdatableInset(in), text_(in.text_.bv_owner)
88 text_.autoBreakRows_ = in.text_.autoBreakRows_;
89 drawFrame_ = in.drawFrame_;
90 frame_color_ = in.frame_color_;
91 text_.paragraphs() = in.text_.paragraphs();
96 InsetText::InsetText() : text_(0)
100 void InsetText::init()
102 for_each(paragraphs().begin(), paragraphs().end(),
103 bind(&Paragraph::setInsetOwner, _1, this));
108 void InsetText::clear(bool just_mark_erased)
110 ParagraphList & pars = paragraphs();
111 if (just_mark_erased) {
112 for_each(pars.begin(), pars.end(),
113 bind(&Paragraph::markErased, _1));
117 // This is a gross hack...
118 LyXLayout_ptr old_layout = pars.begin()->layout();
121 pars.push_back(Paragraph());
122 pars.begin()->setInsetOwner(this);
123 pars.begin()->layout(old_layout);
127 auto_ptr<InsetBase> InsetText::doClone() const
129 return auto_ptr<InsetBase>(new InsetText(*this));
133 void InsetText::write(Buffer const & buf, ostream & os) const
136 text_.write(buf, os);
140 void InsetText::read(Buffer const & buf, LyXLex & lex)
145 #warning John, look here. Doesnt make much sense.
147 if (buf.params().tracking_changes)
148 paragraphs().begin()->trackChanges();
150 // delete the initial paragraph
151 Paragraph oldpar = *paragraphs().begin();
152 paragraphs().clear();
153 bool res = text_.read(buf, lex);
157 lex.printError("Missing \\end_inset at this point. "
162 // ensure we have at least one paragraph.
163 if (paragraphs().empty())
164 paragraphs().push_back(oldpar);
168 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
170 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
171 setViewCache(mi.base.bv);
172 font_ = mi.base.font;
173 text_.font_ = mi.base.font;
174 text_.metrics(mi, dim);
179 void InsetText::draw(PainterInfo & pi, int x, int y) const
181 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
182 // update our idea of where we are
183 setPosCache(pi, x, y);
185 BufferView * bv = pi.base.bv;
189 //y -= text_.ascent();
192 text_.draw(pi, x, y);
195 drawFrame(pi.pain, x, y);
199 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
201 // repaint the background if needed
202 if (backgroundColor() != LColor::background)
203 clearInset(pi.pain, x, y);
204 text_.drawSelection(pi, x, y);
208 void InsetText::drawFrame(Painter & pain, int x, int y) const
210 int const w = max(1, text_.width());
211 int const h = text_.height();
212 int const a = text_.ascent();
213 pain.rectangle(x, y - a, w, h, frameColor());
217 void InsetText::clearInset(Painter & pain, int x, int y) const
219 int const w = text_.width();
220 int const h = text_.height();
221 int const a = text_.ascent();
222 pain.fillRectangle(x, y - a, w, h, backgroundColor());
226 void InsetText::updateLocal(LCursor & cur)
228 if (!text_.autoBreakRows_ && paragraphs().size() > 1) {
229 // collapse paragraphs
230 while (paragraphs().size() > 1) {
231 ParagraphList::iterator const first = paragraphs().begin();
232 ParagraphList::iterator second = first;
234 size_t const first_par_size = first->size();
236 if (!first->empty() &&
238 !first->isSeparator(first_par_size - 1)) {
239 first->insertChar(first_par_size, ' ');
242 cur.clearSelection();
243 mergeParagraph(cur.buffer().params(), paragraphs(), 0);
247 if (!cur.selection())
250 LyXView * lv = cur.bv().owner();
251 lv->view_state_changed();
253 lv->updateToolbars();
254 if (old_pit != cur.pit()) {
255 lv->setLayout(text_.getPar(cur.pit()).layout()->name());
261 string const InsetText::editMessage() const
263 return _("Opened Text Inset");
267 void InsetText::edit(LCursor & cur, bool left)
269 //lyxerr << "InsetText: edit left/right" << endl;
271 setViewCache(&cur.bv());
272 int const pit = left ? 0 : paragraphs().size() - 1;
273 int const pos = left ? 0 : paragraphs().back().size();
274 text_.setCursor(cur.top(), pit, pos);
275 cur.clearSelection();
278 #warning can someone check if/when this is needed?
285 InsetBase * InsetText::editXY(LCursor & cur, int x, int y) const
288 return text_.editXY(cur, x, y);
289 //sanitizeEmptyText(cur.bv());
294 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
296 //lyxerr << "InsetText::doDispatch: " << cmd.action << " " << endl;
297 setViewCache(&cur.bv());
299 text_.dispatch(cur, cmd);
304 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
305 FuncStatus & status) const
307 return text_.getStatus(cur, cmd, status);
311 int InsetText::latex(Buffer const & buf, ostream & os,
312 OutputParams const & runparams) const
315 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
316 return texrow.rows();
320 int InsetText::plaintext(Buffer const & buf, ostream & os,
321 OutputParams const & runparams) const
323 ParagraphList::const_iterator beg = paragraphs().begin();
324 ParagraphList::const_iterator end = paragraphs().end();
325 ParagraphList::const_iterator it = beg;
326 for (; it != end; ++it)
327 asciiParagraph(buf, *it, os, runparams, it == beg);
329 // FIXME: Give the total numbers of lines
334 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
335 OutputParams const & runparams) const
337 linuxdocParagraphs(buf, paragraphs(), os, runparams);
342 int InsetText::docbook(Buffer const & buf, ostream & os,
343 OutputParams const & runparams) const
345 docbookParagraphs(paragraphs(), buf, os, runparams);
350 void InsetText::validate(LaTeXFeatures & features) const
352 for_each(paragraphs().begin(), paragraphs().end(),
353 bind(&Paragraph::validate, _1, ref(features)));
357 void InsetText::getCursorPos(CursorSlice const & sl, int & x, int & y) const
359 x = text_.cursorX(sl);
360 y = text_.cursorY(sl);
364 bool InsetText::showInsetDialog(BufferView *) const
370 void InsetText::getLabelList(Buffer const & buffer,
371 std::vector<string> & list) const
373 ParagraphList::const_iterator pit = paragraphs().begin();
374 ParagraphList::const_iterator pend = paragraphs().end();
375 for (; pit != pend; ++pit) {
376 InsetList::const_iterator beg = pit->insetlist.begin();
377 InsetList::const_iterator end = pit->insetlist.end();
378 for (; beg != end; ++beg)
379 beg->inset->getLabelList(buffer, list);
384 void InsetText::markNew(bool track_changes)
386 ParagraphList::iterator pit = paragraphs().begin();
387 ParagraphList::iterator end = paragraphs().end();
388 for (; pit != end; ++pit) {
392 // no-op when not tracking
399 void InsetText::setText(string const & data, LyXFont const & font)
402 Paragraph & first = paragraphs().front();
403 for (unsigned int i = 0; i < data.length(); ++i)
404 first.insertChar(i, data[i], font);
408 void InsetText::setAutoBreakRows(bool flag)
410 if (flag != text_.autoBreakRows_) {
411 text_.autoBreakRows_ = flag;
418 void InsetText::setDrawFrame(bool flag)
424 LColor_color InsetText::frameColor() const
426 return LColor::color(frame_color_);
430 void InsetText::setFrameColor(LColor_color col)
436 void InsetText::setViewCache(BufferView const * bv) const
438 if (bv && bv != text_.bv_owner) {
439 //lyxerr << "setting view cache from "
440 // << text_.bv_owner << " to " << bv << "\n";
441 text_.bv_owner = const_cast<BufferView *>(bv);
446 void InsetText::removeNewlines()
448 ParagraphList::iterator it = paragraphs().begin();
449 ParagraphList::iterator end = paragraphs().end();
450 for (; it != end; ++it)
451 for (int i = 0; i < it->size(); ++i)
452 if (it->isNewline(i))
457 LyXText * InsetText::getText(int i) const
459 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
463 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
466 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
467 // And it probably does. You have to take a look at this John. (Lgb)
468 #warning John, have a look here. (Lgb)
470 ParagraphList & pl = paragraphs();
472 ParagraphList::iterator pit = plist.begin();
473 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
475 mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
477 for_each(pit, plist.end(),
478 bind(&ParagraphList::push_back, ref(pl), _1));
482 void InsetText::addPreview(PreviewLoader & loader) const
484 ParagraphList::const_iterator pit = paragraphs().begin();
485 ParagraphList::const_iterator pend = paragraphs().end();
487 for (; pit != pend; ++pit) {
488 InsetList::const_iterator it = pit->insetlist.begin();
489 InsetList::const_iterator end = pit->insetlist.end();
490 for (; it != end; ++it)
491 it->inset->addPreview(loader);
496 ParagraphList const & InsetText::paragraphs() const
498 return text_.paragraphs();
502 ParagraphList & InsetText::paragraphs()
504 return text_.paragraphs();