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;
71 InsetText::InsetText(BufferParams const & bp)
72 : autoBreakRows_(false), drawFrame_(NEVER),
73 frame_color_(LColor::insetframe), text_(0)
75 paragraphs().push_back(Paragraph());
76 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
77 if (bp.tracking_changes)
78 paragraphs().back().trackChanges();
83 InsetText::InsetText(InsetText const & in)
84 : UpdatableInset(in), text_(in.text_.bv_owner)
91 InsetText::InsetText() : text_(0)
95 void InsetText::operator=(InsetText const & in)
97 UpdatableInset::operator=(in);
98 autoBreakRows_ = in.autoBreakRows_;
99 drawFrame_ = in.drawFrame_;
100 frame_color_ = in.frame_color_;
101 text_ = LyXText(in.text_.bv_owner);
102 text_.paragraphs() = in.text_.paragraphs();
107 void InsetText::init()
109 ParagraphList::iterator pit = paragraphs().begin();
110 ParagraphList::iterator end = paragraphs().end();
111 for (; pit != end; ++pit)
112 pit->setInsetOwner(this);
117 void InsetText::clear(bool just_mark_erased)
119 ParagraphList & pars = paragraphs();
120 if (just_mark_erased) {
121 ParagraphList::iterator it = pars.begin();
122 ParagraphList::iterator end = pars.end();
123 for (; it != end; ++it)
128 // This is a gross hack...
129 LyXLayout_ptr old_layout = pars.begin()->layout();
132 pars.push_back(Paragraph());
133 pars.begin()->setInsetOwner(this);
134 pars.begin()->layout(old_layout);
138 auto_ptr<InsetBase> InsetText::clone() const
140 return auto_ptr<InsetBase>(new InsetText(*this));
144 void InsetText::write(Buffer const & buf, ostream & os) const
147 text_.write(buf, os);
151 void InsetText::read(Buffer const & buf, LyXLex & lex)
155 #warning John, look here. Doesnt make much sense.
156 if (buf.params().tracking_changes)
157 paragraphs().begin()->trackChanges();
159 // delete the initial paragraph
160 Paragraph oldpar = *paragraphs().begin();
161 paragraphs().clear();
162 bool res = text_.read(buf, lex);
166 lex.printError("Missing \\end_inset at this point. "
171 // ensure we have at least one par.
172 if (paragraphs().empty())
173 paragraphs().push_back(oldpar);
177 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
179 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
180 setViewCache(mi.base.bv);
181 text_.metrics(mi, dim);
183 font_ = mi.base.font;
184 text_.font_ = mi.base.font;
188 void InsetText::draw(PainterInfo & pi, int x, int y) const
190 BOOST_ASSERT(!text_.paragraphs().begin()->rows.empty());
191 // update our idea of where we are
192 setPosCache(pi, x, y);
194 BufferView * bv = pi.base.bv;
200 // repaint the background if needed
201 if (backgroundColor() != LColor::background)
202 clearInset(pi.pain, x, y);
204 text_.draw(pi, x, y + bv->top_y());
206 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
207 drawFrame(pi.pain, x, y);
211 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
213 text_.drawSelection(pi, x, y);
217 void InsetText::drawFrame(Painter & pain, int x, int y) const
219 int const w = text_.width();
220 int const h = text_.height();
221 pain.rectangle(x, y, w, h, frameColor());
225 void InsetText::clearInset(Painter & pain, int x, int y) const
227 int const w = text_.width();
228 int const h = text_.height();
229 pain.fillRectangle(x, y, w, h, backgroundColor());
233 void InsetText::updateLocal(LCursor & cur)
235 if (!autoBreakRows_ && paragraphs().size() > 1) {
236 // collapseParagraphs
237 while (paragraphs().size() > 1) {
238 ParagraphList::iterator const first = paragraphs().begin();
239 ParagraphList::iterator second = first;
241 size_t const first_par_size = first->size();
243 if (!first->empty() &&
245 !first->isSeparator(first_par_size - 1)) {
246 first->insertChar(first_par_size, ' ');
249 cur.clearSelection();
250 mergeParagraph(cur.bv().buffer()->params(), paragraphs(), 0);
254 if (!cur.selection())
257 LyXView * lv = cur.bv().owner();
258 lv->view_state_changed();
261 if (old_par != cur.par()) {
262 lv->setLayout(text_.getPar(cur.par()).layout()->name());
268 string const InsetText::editMessage() const
270 return _("Opened Text Inset");
274 void InsetText::sanitizeEmptyText(BufferView & bv)
276 if (paragraphs().size() == 1
277 && paragraphs().begin()->empty()
278 && bv.getParentLanguage(this) != text_.current_font.language()) {
279 LyXFont font(LyXFont::ALL_IGNORE);
280 font.setLanguage(bv.getParentLanguage(this));
281 text_.setFont(bv.cursor(), font, false);
286 void InsetText::edit(LCursor & cur, bool left)
288 //lyxerr << "InsetText: edit left/right" << endl;
290 setViewCache(&cur.bv());
291 int const par = left ? 0 : paragraphs().size() - 1;
292 int const pos = left ? 0 : paragraphs().back().size();
293 text_.setCursor(cur.top(), par, pos);
294 cur.clearSelection();
296 sanitizeEmptyText(cur.bv());
297 #warning can someone check if/when this is needed?
303 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
305 lyxerr << "InsetText::edit xy" << endl;
307 return text_.editXY(cur, x, y);
308 //sanitizeEmptyText(cur.bv());
313 void InsetText::priv_dispatch(LCursor & cur, FuncRequest & cmd)
315 //lyxerr << "InsetText::priv_dispatch: " << cmd.action << " " << endl;
316 setViewCache(&cur.bv());
318 bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
319 text_.dispatch(cur, cmd);
321 // If the action has deleted all text in the inset, we need
322 // to change the language to the language of the surronding
324 // Why this cleverness? (Andre')
325 if (!was_empty && paragraphs().begin()->empty() &&
326 paragraphs().size() == 1) {
327 LyXFont font(LyXFont::ALL_IGNORE);
328 font.setLanguage(cur.bv().getParentLanguage(this));
329 text_.setFont(cur, font, false);
334 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
335 FuncStatus & status) const
337 return text_.getStatus(cur, cmd, status);
341 int InsetText::latex(Buffer const & buf, ostream & os,
342 OutputParams const & runparams) const
345 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
346 return texrow.rows();
350 int InsetText::plaintext(Buffer const & buf, ostream & os,
351 OutputParams const & runparams) const
353 ParagraphList::const_iterator beg = paragraphs().begin();
354 ParagraphList::const_iterator end = paragraphs().end();
355 ParagraphList::const_iterator it = beg;
356 for (; it != end; ++it)
357 asciiParagraph(buf, *it, os, runparams, it == beg);
359 //FIXME: Give the total numbers of lines
364 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
365 OutputParams const & runparams) const
367 linuxdocParagraphs(buf, paragraphs(), os, runparams);
372 int InsetText::docbook(Buffer const & buf, ostream & os,
373 OutputParams const & runparams) const
375 docbookParagraphs(buf, paragraphs(), os, runparams);
380 void InsetText::validate(LaTeXFeatures & features) const
382 for_each(paragraphs().begin(), paragraphs().end(),
383 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
387 void InsetText::getCursorPos(CursorSlice const & cur, int & x, int & y) const
389 x = text_.cursorX(cur);
390 y = text_.cursorY(cur);
394 bool InsetText::showInsetDialog(BufferView *) const
400 void InsetText::getLabelList(Buffer const & buffer,
401 std::vector<string> & list) const
403 ParagraphList::const_iterator pit = paragraphs().begin();
404 ParagraphList::const_iterator pend = paragraphs().end();
405 for (; pit != pend; ++pit) {
406 InsetList::const_iterator beg = pit->insetlist.begin();
407 InsetList::const_iterator end = pit->insetlist.end();
408 for (; beg != end; ++beg)
409 beg->inset->getLabelList(buffer, list);
414 void InsetText::markNew(bool track_changes)
416 ParagraphList::iterator pit = paragraphs().begin();
417 ParagraphList::iterator end = paragraphs().end();
418 for (; pit != end; ++pit) {
422 // no-op when not tracking
429 void InsetText::setText(string const & data, LyXFont const & font)
432 for (unsigned int i = 0; i < data.length(); ++i)
433 paragraphs().begin()->insertChar(i, data[i], font);
437 void InsetText::setAutoBreakRows(bool flag)
439 if (flag != autoBreakRows_) {
440 autoBreakRows_ = flag;
447 void InsetText::setDrawFrame(DrawFrame how)
453 LColor_color InsetText::frameColor() const
455 return LColor::color(frame_color_);
459 void InsetText::setFrameColor(LColor_color col)
465 void InsetText::setViewCache(BufferView const * bv) const
467 if (bv && bv != text_.bv_owner) {
468 //lyxerr << "setting view cache from "
469 // << text_.bv_owner << " to " << bv << "\n";
470 text_.bv_owner = const_cast<BufferView *>(bv);
475 void InsetText::removeNewlines()
477 ParagraphList::iterator it = paragraphs().begin();
478 ParagraphList::iterator end = paragraphs().end();
479 for (; it != end; ++it)
480 for (int i = 0; i < it->size(); ++i)
481 if (it->isNewline(i))
486 LyXText * InsetText::getText(int i) const
488 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
492 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
494 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
495 // And it probably does. You have to take a look at this John. (Lgb)
496 #warning John, have a look here. (Lgb)
497 ParagraphList::iterator pit = plist.begin();
498 ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
500 mergeParagraph(buffer->params(), paragraphs(),
501 ins - paragraphs().begin() - 1);
503 ParagraphList::iterator pend = plist.end();
504 for (; pit != pend; ++pit)
505 paragraphs().push_back(*pit);
509 void InsetText::addPreview(PreviewLoader & loader) const
511 ParagraphList::const_iterator pit = paragraphs().begin();
512 ParagraphList::const_iterator pend = paragraphs().end();
514 for (; pit != pend; ++pit) {
515 InsetList::const_iterator it = pit->insetlist.begin();
516 InsetList::const_iterator end = pit->insetlist.end();
517 for (; it != end; ++it)
518 it->inset->addPreview(loader);
523 ParagraphList const & InsetText::paragraphs() const
525 return text_.paragraphs();
529 ParagraphList & InsetText::paragraphs()
531 return text_.paragraphs();