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 : autoBreakRows_(false), drawFrame_(false),
76 frame_color_(LColor::insetframe), text_(0)
78 paragraphs().push_back(Paragraph());
79 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
80 if (bp.tracking_changes)
81 paragraphs().back().trackChanges();
86 InsetText::InsetText(InsetText const & in)
87 : UpdatableInset(in), text_(in.text_.bv_owner)
89 autoBreakRows_ = in.autoBreakRows_;
90 drawFrame_ = in.drawFrame_;
91 frame_color_ = in.frame_color_;
92 text_.paragraphs() = in.text_.paragraphs();
97 InsetText::InsetText() : text_(0)
101 void InsetText::init()
103 for_each(paragraphs().begin(), paragraphs().end(),
104 bind(&Paragraph::setInsetOwner, _1, this));
109 void InsetText::clear(bool just_mark_erased)
111 ParagraphList & pars = paragraphs();
112 if (just_mark_erased) {
113 for_each(pars.begin(), pars.end(),
114 bind(&Paragraph::markErased, _1));
118 // This is a gross hack...
119 LyXLayout_ptr old_layout = pars.begin()->layout();
122 pars.push_back(Paragraph());
123 pars.begin()->setInsetOwner(this);
124 pars.begin()->layout(old_layout);
128 auto_ptr<InsetBase> InsetText::doClone() const
130 return auto_ptr<InsetBase>(new InsetText(*this));
134 void InsetText::write(Buffer const & buf, ostream & os) const
137 text_.write(buf, os);
141 void InsetText::read(Buffer const & buf, LyXLex & lex)
146 #warning John, look here. Doesnt make much sense.
148 if (buf.params().tracking_changes)
149 paragraphs().begin()->trackChanges();
151 // delete the initial paragraph
152 Paragraph oldpar = *paragraphs().begin();
153 paragraphs().clear();
154 bool res = text_.read(buf, lex);
158 lex.printError("Missing \\end_inset at this point. "
163 // ensure we have at least one paragraph.
164 if (paragraphs().empty())
165 paragraphs().push_back(oldpar);
169 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
171 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
172 setViewCache(mi.base.bv);
173 font_ = mi.base.font;
174 text_.font_ = mi.base.font;
175 text_.metrics(mi, dim);
180 void InsetText::draw(PainterInfo & pi, int x, int y) const
182 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
183 // update our idea of where we are
184 setPosCache(pi, x, y);
186 BufferView * bv = pi.base.bv;
190 //y -= text_.ascent();
193 text_.draw(pi, x, y);
196 drawFrame(pi.pain, x, y);
200 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
202 // repaint the background if needed
203 if (backgroundColor() != LColor::background)
204 clearInset(pi.pain, x, y);
205 text_.drawSelection(pi, x, y);
209 void InsetText::drawFrame(Painter & pain, int x, int y) const
211 int const w = max(1, text_.width());
212 int const h = text_.height();
213 int const a = text_.ascent();
214 pain.rectangle(x, y - a, w, h, frameColor());
218 void InsetText::clearInset(Painter & pain, int x, int y) const
220 int const w = text_.width();
221 int const h = text_.height();
222 int const a = text_.ascent();
223 pain.fillRectangle(x, y - a, w, h, backgroundColor());
227 void InsetText::updateLocal(LCursor & cur)
229 if (!autoBreakRows_ && paragraphs().size() > 1) {
230 // collapse paragraphs
231 while (paragraphs().size() > 1) {
232 ParagraphList::iterator const first = paragraphs().begin();
233 ParagraphList::iterator second = first;
235 size_t const first_par_size = first->size();
237 if (!first->empty() &&
239 !first->isSeparator(first_par_size - 1)) {
240 first->insertChar(first_par_size, ' ');
243 cur.clearSelection();
244 mergeParagraph(cur.buffer().params(), paragraphs(), 0);
248 if (!cur.selection())
251 LyXView * lv = cur.bv().owner();
252 lv->view_state_changed();
254 lv->updateToolbars();
255 if (old_pit != cur.pit()) {
256 lv->setLayout(text_.getPar(cur.pit()).layout()->name());
262 string const InsetText::editMessage() const
264 return _("Opened Text Inset");
268 void InsetText::edit(LCursor & cur, bool left)
270 //lyxerr << "InsetText: edit left/right" << endl;
272 setViewCache(&cur.bv());
273 int const pit = left ? 0 : paragraphs().size() - 1;
274 int const pos = left ? 0 : paragraphs().back().size();
275 text_.setCursor(cur.top(), pit, pos);
276 cur.clearSelection();
279 #warning can someone check if/when this is needed?
286 InsetBase * InsetText::editXY(LCursor & cur, int x, int y) const
289 return text_.editXY(cur, x, y);
290 //sanitizeEmptyText(cur.bv());
295 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
297 //lyxerr << "InsetText::doDispatch: " << cmd.action << " " << endl;
298 setViewCache(&cur.bv());
300 bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
301 text_.dispatch(cur, cmd);
306 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
307 FuncStatus & status) const
309 return text_.getStatus(cur, cmd, status);
313 int InsetText::latex(Buffer const & buf, ostream & os,
314 OutputParams const & runparams) const
317 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
318 return texrow.rows();
322 int InsetText::plaintext(Buffer const & buf, ostream & os,
323 OutputParams const & runparams) const
325 ParagraphList::const_iterator beg = paragraphs().begin();
326 ParagraphList::const_iterator end = paragraphs().end();
327 ParagraphList::const_iterator it = beg;
328 for (; it != end; ++it)
329 asciiParagraph(buf, *it, os, runparams, it == beg);
331 // FIXME: Give the total numbers of lines
336 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
337 OutputParams const & runparams) const
339 linuxdocParagraphs(buf, paragraphs(), os, runparams);
344 int InsetText::docbook(Buffer const & buf, ostream & os,
345 OutputParams const & runparams) const
347 docbookParagraphs(paragraphs(), buf, os, runparams);
352 void InsetText::validate(LaTeXFeatures & features) const
354 for_each(paragraphs().begin(), paragraphs().end(),
355 bind(&Paragraph::validate, _1, ref(features)));
359 void InsetText::getCursorPos(CursorSlice const & sl, int & x, int & y) const
361 x = text_.cursorX(sl);
362 y = text_.cursorY(sl);
366 bool InsetText::showInsetDialog(BufferView *) const
372 void InsetText::getLabelList(Buffer const & buffer,
373 std::vector<string> & list) const
375 ParagraphList::const_iterator pit = paragraphs().begin();
376 ParagraphList::const_iterator pend = paragraphs().end();
377 for (; pit != pend; ++pit) {
378 InsetList::const_iterator beg = pit->insetlist.begin();
379 InsetList::const_iterator end = pit->insetlist.end();
380 for (; beg != end; ++beg)
381 beg->inset->getLabelList(buffer, list);
386 void InsetText::markNew(bool track_changes)
388 ParagraphList::iterator pit = paragraphs().begin();
389 ParagraphList::iterator end = paragraphs().end();
390 for (; pit != end; ++pit) {
394 // no-op when not tracking
401 void InsetText::setText(string const & data, LyXFont const & font)
404 Paragraph & first = paragraphs().front();
405 for (unsigned int i = 0; i < data.length(); ++i)
406 first.insertChar(i, data[i], font);
410 void InsetText::setAutoBreakRows(bool flag)
412 if (flag != autoBreakRows_) {
413 autoBreakRows_ = flag;
420 void InsetText::setDrawFrame(bool flag)
426 LColor_color InsetText::frameColor() const
428 return LColor::color(frame_color_);
432 void InsetText::setFrameColor(LColor_color col)
438 void InsetText::setViewCache(BufferView const * bv) const
440 if (bv && bv != text_.bv_owner) {
441 //lyxerr << "setting view cache from "
442 // << text_.bv_owner << " to " << bv << "\n";
443 text_.bv_owner = const_cast<BufferView *>(bv);
448 void InsetText::removeNewlines()
450 ParagraphList::iterator it = paragraphs().begin();
451 ParagraphList::iterator end = paragraphs().end();
452 for (; it != end; ++it)
453 for (int i = 0; i < it->size(); ++i)
454 if (it->isNewline(i))
459 LyXText * InsetText::getText(int i) const
461 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
465 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
468 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
469 // And it probably does. You have to take a look at this John. (Lgb)
470 #warning John, have a look here. (Lgb)
472 ParagraphList & pl = paragraphs();
474 ParagraphList::iterator pit = plist.begin();
475 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
477 mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
479 for_each(pit, plist.end(),
480 bind(&ParagraphList::push_back, ref(pl), _1));
484 void InsetText::addPreview(PreviewLoader & loader) const
486 ParagraphList::const_iterator pit = paragraphs().begin();
487 ParagraphList::const_iterator pend = paragraphs().end();
489 for (; pit != pend; ++pit) {
490 InsetList::const_iterator it = pit->insetlist.begin();
491 InsetList::const_iterator end = pit->insetlist.end();
492 for (; it != end; ++it)
493 it->inset->addPreview(loader);
498 ParagraphList const & InsetText::paragraphs() const
500 return text_.paragraphs();
504 ParagraphList & InsetText::paragraphs()
506 return text_.paragraphs();